Get length of an Array using a pointer

后端 未结 6 1799
广开言路
广开言路 2021-01-03 05:25

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] =         


        
相关标签:
6条回答
  • 2021-01-03 06:00

    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) );
    
    0 讨论(0)
  • 2021-01-03 06:03

    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.

    0 讨论(0)
  • 2021-01-03 06:11

    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...

    0 讨论(0)
  • 2021-01-03 06:12
    • 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.
    0 讨论(0)
  • 2021-01-03 06:13

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

    if in C++ use vector class

    0 讨论(0)
  • 2021-01-03 06:16

    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.

    0 讨论(0)
提交回复
热议问题