How do I find the length of an array?

前端 未结 27 2737
轮回少年
轮回少年 2020-11-21 23:10

Is there a way to find how many values an array has? Detecting whether or not I\'ve reached the end of an array would also work.

27条回答
  •  有刺的猬
    2020-11-21 23:37

    I provide a tricky solution here:

    You can always store length in the first element:

    // malloc/new
    
    arr[0] = length;
    arr++;
    
    // do anything. 
    int len = *(arr-1);
    
    free(--arr); 
    

    The cost is you must --arr when invoke free

提交回复
热议问题