如何在C / Objective-C中获取数组的大小?

时间:2021-11-05 19:48:03

I have this array:

我有这个数组:

unsigned char* data = CGBitmapContextGetData(cgctx);

then I tried to get the size with sizeof(data), but that will return me a nonsense-value of 4. data holds a big amount of information. That can't be just 4 ;)

然后我试着用sizeof(数据)获得大小,但这将返回一个无意义值为4.数据包含大量信息。这不能只是4;)

I even get information at data[8293] ... so ... not 4 elements at all.

我甚至可以获得数据[8293]的信息...所以...根本不是4个元素。

5 个解决方案

#1


sizeof returns 4 because your variable is declared as a pointer, as opposed to a C array (char data[1024]).

sizeof返回4,因为您的变量被声明为指针,而不是C数组(char数据[1024])。

To get the size you need to use CGBitmapContextGetHeight and CGBitmapContextGetWidth.

要获得您需要使用CGBitmapContextGetHeight和CGBitmapContextGetWidth的大小。

#2


Try to use

尝试使用

CGBitmapContextGetBitsPerPixel
CGBitmapContextGetHeight
CGBitmapContextGetWidth

or

CGBitmapContextGetBytesPerRow
CGBitmapContextGetHeight

#3


The value of 4 is not nonsense, it's the size of the pointer you asked for. There is no way in C of finding out then size of the array that a pointer points to.

4的值不是废话,它是你要求的指针的大小。在C中找不到指针所指向的数组的大小是没有办法的。

#4


You're taking the size of the pointer, not of the array. Using sizeof() for arrays only work if you actually have an array to measure ;)

你拿的是指针的大小,而不是数组的大小。对数组使用sizeof()只有在实际有一个要测量的数组时才有效;)

#5


You're confusing a pointer and the object it points to.

你把指针和它指向的对象混淆了。

#1


sizeof returns 4 because your variable is declared as a pointer, as opposed to a C array (char data[1024]).

sizeof返回4,因为您的变量被声明为指针,而不是C数组(char数据[1024])。

To get the size you need to use CGBitmapContextGetHeight and CGBitmapContextGetWidth.

要获得您需要使用CGBitmapContextGetHeight和CGBitmapContextGetWidth的大小。

#2


Try to use

尝试使用

CGBitmapContextGetBitsPerPixel
CGBitmapContextGetHeight
CGBitmapContextGetWidth

or

CGBitmapContextGetBytesPerRow
CGBitmapContextGetHeight

#3


The value of 4 is not nonsense, it's the size of the pointer you asked for. There is no way in C of finding out then size of the array that a pointer points to.

4的值不是废话,它是你要求的指针的大小。在C中找不到指针所指向的数组的大小是没有办法的。

#4


You're taking the size of the pointer, not of the array. Using sizeof() for arrays only work if you actually have an array to measure ;)

你拿的是指针的大小,而不是数组的大小。对数组使用sizeof()只有在实际有一个要测量的数组时才有效;)

#5


You're confusing a pointer and the object it points to.

你把指针和它指向的对象混淆了。