0

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?

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166

5 Answers5

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()

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
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>.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

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

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
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).

Jason
  • 31,834
  • 7
  • 59
  • 78
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

Malcolm Box
  • 3,968
  • 1
  • 26
  • 44
  • Err, arrays aren't pointers, they just decay to pointers in a great many contexts. – Chris Lutz Sep 07 '11 at 15:38
  • 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. – Malcolm Box Sep 07 '11 at 15:48
  • 1
    `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. – Chris Lutz Sep 07 '11 at 15:55