Sizeof C 整数数组(在 Obj-C 中)
myclass.h:
#define BUTTON_NAVI 41;
#define BUTTON_SETTINGS 42;
#define BUTTON_INFO 43;
myclass.m:
int btnNavi = BUTTON_NAVI;
int btnSettings = BUTTON_SETTINGS;
int btnArray[2] = {btnNavi, btnSettings};
NSLog(@"count = %i", sizeof(btnArray));
[self addToolbarButtons:btnArray];
->日志:计数 = 8
8?!我做错了什么?
在“addToolbarButtons”内部计数为 4...:-(
编辑:
- (void)addToolbarButtons:(int[])buttonIdArray {
NSLog(@"count = %i", sizeof(buttonIdArray));
}
-> 日志:计数 = 4
myclass.h:
#define BUTTON_NAVI 41;
#define BUTTON_SETTINGS 42;
#define BUTTON_INFO 43;
myclass.m:
int btnNavi = BUTTON_NAVI;
int btnSettings = BUTTON_SETTINGS;
int btnArray[2] = {btnNavi, btnSettings};
NSLog(@"count = %i", sizeof(btnArray));
[self addToolbarButtons:btnArray];
-> Log: count = 8
8?! What did I do wrong?
And inside "addToolbarButtons" count is 4... :-(
EDIT:
- (void)addToolbarButtons:(int[])buttonIdArray {
NSLog(@"count = %i", sizeof(buttonIdArray));
}
-> Log: count = 4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sizeof
给出了以字节为单位的大小,8 个字节听起来适合 2 个整数(每个 32 位或 4 个字节)。如果您想要的是数组的长度,您可以执行
sizeof(arr) / sizeof(arr[0])
,这将为您提供整个数组的大小除以每个元素的大小。在这种情况下,您将得到8 / 4 == 2
,这正是我所期望的。编辑
为了回答你的第二个问题,当你将数组传递给方法时,你实际上是在传递一个指向数组的指针。因此,指针的大小也是 32 位或 4 字节。如果您希望函数知道数组的长度,则还需要将其长度与所述指针一起传递。
sizeof
is giving you the size in bytes, 8 bytes sounds right for 2 integers (32-bit or 4 bytes each).If what you want is the length of the array, you can do
sizeof(arr) / sizeof(arr[0])
which will give you the size of the entire array divided by the size of each element. In this case you will get8 / 4 == 2
, which is what I take it you expect.EDIT
To answer your second question, when you pass the array to the method, you're actually passing a pointer to the array. Hence, the size of the pointer is also 32-bits or 4 bytes. If you want the function to know the length of the array, you need to also pass its length along with said pointer.
sizeof
给出数组的大小(以字节为单位)。一个 int 是 4 个字节,因此 2 元素的 int 数组将是 8 个字节。但是,
sizeof
不会在您的方法中执行您想要的操作。当 C 数组传递到函数(或方法)时,它实际上是作为指针传递的,并且 sizeof 将返回指针的大小。您应该修改您的方法以采用长度参数:sizeof
is giving the size of the array in bytes. An int is 4 bytes, so a 2-element array of ints will be 8 bytes.However,
sizeof
won't do what you want in your method. When a C array is passed into a function (or method), it actually get passed as a pointer, andsizeof
will return the size of a pointer. You should modify your method to take a length parameter:sizeof
运算符给出传递给它的内容的大小(以字节为单位)。如果一个int
是 4 个字节,那么两个int
的数组就是 8 个字节。然而,
sizeof
是对变量本身的编译时检查。在运行时,数组的边界是未知的——在定义变量的范围之外也不知道它们。当您声明参数(int[])buttonIdArray
时,编译器会将其脱糖为(int *)buttonArray
— 它只是一个普通的 int 指针。因此,当您执行sizeof(buttonArray)
时,它会告诉您指针的大小,4。因为该语言不会为您跟踪它们的大小,所以使用 C 数组很痛苦。您必须将数组中的元素数量传递给对其进行操作的每个函数和方法。
The
sizeof
operator gives you the size in bytes of the thing you pass to it. If anint
is 4 bytes, then an array of twoint
s is 8 bytes.However,
sizeof
is a compile-time check on the variable itself. At runtime, the bounds of an array are not known — nor are they known outside the scope where the variable was defined. When you declare the argument(int[])buttonIdArray
, the compiler desugars that to(int *)buttonArray
— it's just a plain int pointer. So when you dosizeof(buttonArray)
, it tells you the size of a pointer, 4.Because the language doesn't keep track of their size for you, using C arrays is a pain. You have to pass the number of elements in the array to every function and method that acts on it.