How to calculate size of array from pointer variable?

前端 未结 3 1016
南旧
南旧 2020-12-06 15:22

i have pointer of array(array which is in memory). Can i calculate the size of array from its pointer ? i dont know actually where is the array in memory. i only getting poi

3条回答
  •  佛祖请我去吃肉
    2020-12-06 16:12

    No. arrays in C doesn't have this info. you should hold a variable "next" to the array with this data.

    if you have the array it self you can use the sizeof to get his size in compilation stage.

    char array1[XXX];
    char* pointer1 = array1;
    sizeof(array1); // this is XXX
    sizeof(pointer1); // this is the size of a pointer in your system
    

    you should do the following and use the variable in you program (or pass it to function when you passing the array to a function)

    int array1size = sizeof(array1);
    

提交回复
热议问题