简体   繁体   English

在不使用sizeof的情况下查找数组的大小

[英]Find size of array without using sizeof

I was searching for a way to find the size of an array in C without using sizeof and I found the following code: 我正在寻找一种在不使用sizeof情况下在C中查找数组大小的方法,我找到了以下代码:

int main ()
{
    int arr[100];
    printf ("%d\n", (&arr)[1] - arr);
    return 0;
}

Can anyone please explain to me how is it working? 任何人都可以向我解释它是如何工作的?

&arr is a pointer to an array of 100 int s. &arr是指向100个int的数组的指针。

The [1] means "add the size of the thing that is pointed to", which is an array of 100 int s. [1]表示“添加指向的东西的大小”,这是一个100个int的数组。

So the difference between (&arr)[1] and arr is 100 int s. 所以(&arr)[1]arr之间的差异是100 int s。

(Note that this trick will only work in places where sizeof would have worked anyway.) (请注意,此技巧仅适用于sizeof无论如何都会工作的地方。)

&arr gives you a pointer to the array. &arr为您提供指向数组的指针。 (&arr)[1] is equivalent to *(&arr + 1) . (&arr)[1]相当于*(&arr + 1) &arr + 1 gives you a pointer to the array of 100 ints that follows arr . &arr + 1为您提供指向arr的100个int数组的指针。 Dereferencing it with * gives you that array that follows. *取消引用它会为您提供随后的数组。 Since this array is used in an additive expression ( - ), it decays to the pointer to its first element. 由于此数组用于加法表达式( - ),因此它会衰减到指向其第一个元素的指针。 The same happens to arr in the expression. 表达式中的arr也是如此。 So you subtract to pointers, one pointing to the non-existent element right after arr and the other pointing to the first element of arr . 所以你减去指针,一个指向arr后面不存在的元素,另一个指向arr的第一个元素。 This gives you 100. 这给你100。

But it's not working. 但它不起作用。 %d is used for int . %d用于int Pointer difference returns you ptrdiff_t and not int . 指针差异会返回ptrdiff_t而不是int You need to use %td for ptrdiff_t . 您需要将%td用于ptrdiff_t If you lie to printf() about the types of the parameters you're passing to it, you get well-deserved undefined behavior. 如果你对printf()说谎你传递给它的参数的类型,你会得到当之无愧的未定义行为。

EDIT : (&arr)[1] may cause undefined behavior. 编辑(&arr)[1] 可能会导致未定义的行为。 It's not entirely clear. 这还不完全清楚。 See the comments below, if interested. 如果有兴趣,请参阅下面的评论。

Generally (as per visual studio), for an array &arr is same as arr ,which return the starting base address of our function. 通常(根据视觉工作室),用于阵列&arr是相同arr ,其返回我们的函数的起始基地址。

(&arr)[0] is nothing but &arr or arr (&arr)[0]只是&arrarr

ex: it will return some address : 1638116 例如:它将返回一些地址: 1638116

Now, (&arr)[1] means we are started accessing the array out of bounce means next array or next segment of the size of present array(100 ahead). 现在, (&arr)[1]意味着我们开始从弹跳中访问数组意味着下一个数组或当前数组大小的下一个段(前面100个)。

ex: it will return some address : 1638216 例如:它将返回一些地址: 1638216

Now, subtracting (&arr)[1] - (&arr)[0]=100 现在,减去(&arr)[1] - (&arr)[0]=100

&variable gives location of the variable (call it as P ) &variable给出&variable位置(称为P
&variable + 1 gives address of the location next to the variable. &variable + 1给出变量旁边位置的地址。 (call it as N ) (称之为N

(char*)N-(char*)P gives how many characters are there between N and P . (char*)N-(char*)P给出NP之间有多少个字符。 Since each character is 1 byte sized, so the above result gives the number of bytes P and N . 由于每个字符的大小为1字节,因此上述结果给出了字节数PN (which equals to the size of array in bytes). (等于以字节为单位的数组大小)。

Similarly, (char*) (a+1)-(char*)a; 同样, (char*) (a+1)-(char*)a; gives size of each element of the array in bytes. 给出数组中每个元素的大小(以字节为单位)。

So the number of elements in the array = (size of array in bytes)/(size of each element in the array in bytes) 所以数组中的元素数量= (size of array in bytes)/(size of each element in the array in bytes)数组的(size of array in bytes)/(size of each element in the array in bytes)

#include<stdio.h>

int main()
{
    int a[100];
    int b = ((char*)(&a+1)-(char*)(&a));
    int c = (char*) (a+1)-(char*)a;
    b = b/c;
    printf("The size of array should be %d",b);
    return 0;

}

int arry [6] = {1,2,3,4,5,6} //让数组元素为6,所以......大小在byte =(char *)(arry + 6) - (char *)( ARRY)= 24;

int main ()
{
  int arr[100];
  printf ("%d\n", ((char*)(&arr+1) - (char*)(&arr))/((char*) (arr+1) -(char*) (arr)));
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM