Get length of an Array using a pointer

后端 未结 6 1827
广开言路
广开言路 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) );
    

提交回复
热议问题