1

So, given the following:

#include <stdio.h> int * getarr(); int main(int argc, char* argv) { int * arr = getarr(); printf("%d", sizeof(arr)); } int* getarr() { static int a[4] = {0,1,0,3}; return a; } 

How does one find the length of arr? arr[4] == 0, but so does arr[0] and arr[2].

If this were a char*, the answer would be iterate until '\0', but that does not seem to work here as '\0' == 0.

Addressing arr[5] can seems to consistently result in a value > 163 - 1 (the size of an int on my system), but that does not seem to be a reliable measure as it strikes me as simply an empty location in memory.

Is there a way to retrieve this value consistently? Or does it simply have to be passed in?

3
  • 4
    Accessing arr[4] and above is undefined behavior. Commented Sep 7, 2011 at 15:36
  • @Mat, I had believed so but it seemed like it might be relevant. Commented Sep 7, 2011 at 15:38
  • Same as: stackoverflow.com/questions/2089071/array-length-in-c Commented Sep 7, 2011 at 15:44

5 Answers 5

4

You cannot retreive the length of the array when you are in the main() function. This information has been lost when the int[4] was converted to an int * returned by getarr()

Sign up to request clarification or add additional context in comments.

Comments

3

You have to keep track of the size of the array yourself. There is nothing in the language that will do that for you. Sorry.

This is one of the reasons why people use higher level languages that contain more powerful data structures, e.g. std::vector<T>.

Comments

2

You can't find the length of that array, besides explicitly passing the size with it.

Comments

1

C arrays are just strings of bytes in memory ... they are not like Pascal strings or other "array-like" data-structures in languages like Java, Python, etc. that have run-time bounds checking, and therefore couple information about the size of the array with the actual array data. Therefore you are going to have to pass the size of the array around in order to know how large it is, unless it was allocated statically, or on the local stack frame, at which point you could use sizeof(array).

Comments

0

There's no way to know in C. A C array is just a pointer to memory, so it has the same size as the underlying pointer type.

Thus you have to have the length stored somewhere else, e.g. in a constant

3 Comments

Err, arrays aren't pointers, they just decay to pointers in a great many contexts.
You're right, an array isn't a pointer. But the types of the identifiers that reference both are the same ie the type of a declared as int a[5] is the same as int * b? If it walks like a duck and quacks like a duck, I call it a duck.
sizeof(int [30]) != sizeof(int *) on any system I know of. Therefore, the types are not the same. You can make a pointer to an array (int (*)[30]) and it's clearly not the same as a pointer to a pointer (int **) - both can be indexed twice, but the former when indexed yields a block of contiguous data that can be indexed, while the latter yields a pointer to contiguous data elsewhere that can be indexed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.