Get length of an Array using a pointer

后端 未结 6 1834
广开言路
广开言路 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: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.

提交回复
热议问题