0

In C, if we declare an array, we can use sizeof() to get the length of the array. If I get a pointer, sizeof() will return the size of pointer to me, so how I can get the size of array pointed by the pointer or how to set the pointer to the last position? I know char * can use strlen, what about other type?

Thanks

Dylan Wang
  • 86
  • 1
  • 8
  • You can't, you have to maintain a variable or constant giving the size yourself. By the way, `strlen` only gives you the number of characters up to the binary zero. The array holding the string could be much larger. – cdarke Apr 17 '18 at 06:52
  • You have to store the size before `malloc` (or other allocation function) is called. BTW, except for the VLA, the value of sizeof() is determined after compilation, which means you can't use it to dynamically measure the size of something. – Stan Apr 17 '18 at 06:54

0 Answers0