speendo
speendo

Reputation: 13345

Get length of an Array using a pointer

Is there a way to get the length of an Array when I only know a pointer pointing to the Array?

See the following example

int testInt[3];
testInt[0] = 0;
testInt[1] = 1;
testInt[2] = 1;

int* point;
point = testInt;

Serial.println(sizeof(testInt) / sizeof(int)); // returns 3
Serial.println(sizeof(point) / sizeof(int)); // returns 1

(This is a snipplet from Arduino Code - I'm sorry, I don't "speak" real C).

Upvotes: 6

Views: 23389

Answers (6)

chux
chux

Reputation: 154198

Is there a way to get the length of an Array when I only know a pointer pointing to the Array?

Technically yes, there is a way when code has a true pointer to an array as the array size is in the type as with int (*array_pointer)[3].

This differs from OP's code as the pointer point is not a pointer to an array, but a pointer to an int.

The line point = testInt; converts the array testInt to the address of the first element of the array (which is an int *) and assigns that to point. Thus the array size info is lost.

int testInt[3];
testInt[0] = 0;
testInt[1] = 1;
testInt[2] = 1;

int* point;
point = testInt;                     // Get the address of testInt[0]

int (*array_pointer)[3] = &testInt;  // Get the address of the array

printf("%zu\n", sizeof(testInt) / sizeof(int));
printf("%zu\n", sizeof(point) / sizeof(int));
printf("%zu\n", sizeof(*point) / sizeof(int));
printf("%zu\n", sizeof(*array_pointer) / sizeof(int));

printf("%p\n", (void *) testInt);
printf("%p\n", (void *) point);
printf("%p\n", (void *) array_pointer);

Sample output

3
2
1
3

0xffffcbc4
0xffffcbc4
0xffffcbc4

Pointers point and array_pointer both have values that point to the same location in memory, but the pointers differ in type.


With C99 or later that support variable length arrays, code could have been the below and achieved similar results without explicitly coding a 3 in the pointer definition.

int (*array_pointer_vla)[sizeof testInt/sizeof testInt[0]] = &testInt;
printf("%zu\n", sizeof(*array_pointer_vla) / sizeof(int));

Output

3

I see now see similarities to @user411313 answer. Perhaps the deeper explanation and VLA discussion will be useful.

Upvotes: 0

Mik Wind
Mik Wind

Reputation: 51

Also doing an Arduino project here... Everybody on the internet seems to insist it's impossible to do this... and yet the oldest trick in the book seems to work just fine with null terminated arrays...

example for char pointer:

    int getSize(char* ch){
      int tmp=0;
      while (*ch) {
        *ch++;
        tmp++;
      }return tmp;}

magic...

Upvotes: 5

user411313
user411313

Reputation: 3990

You can if you point the the whole array and NOT point to the first element like:

int testInt[3];
int (*point)[3];
point = testInt;

printf( "number elements: %lu", (unsigned long)(sizeof*point/sizeof**point) );
printf( "whole array size: %lu", (unsigned long)(sizeof*point) );

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613461

  • You can infer the length of an array if you have an array variable.
  • You cannot infer the length of an array if you have just a pointer to it.

Upvotes: 3

forvaidya
forvaidya

Reputation: 3315

You cannot and you should not attempt deduce array length using pointer arithmetic

if in C++ use vector class

Upvotes: 1

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

The easy answer is no, you cannot. You'll probably want to keep a variable in memory which stores the amount of items in the array.

And there's a not-so-easy answer. There's a way to determine the length of an array, but for that you would have to mark the end of the array with another element, such as -1. Then just loop through it and find this element. The position of this element is the length. However, this won't work with your current code.

Pick one of the above.

Upvotes: 13

Related Questions