sizeof(int) 和 sizeof(int*) 有什么区别?另外,这个语句 int*numbers[] = {....} 正确吗?
假设,
int numbers [20];
int * p;
我认为这个 is 语句是有效的
p = numbers;
,但这不是
numbers = p;
因为 numbers
是一个数组,作为常量指针操作,并且我们不能为常量赋值。那么如果我们这样做的话,那么我们在初始化数组时就不能使用 *numbers
吗?
Suppose,
int numbers [20];
int * p;
I think this is statement is valid
p = numbers;
But this is not
numbers = p;
Because numbers
is an array, operates as a constant pointer, and we cannot assign values to constants. So if we go by this then we cannot use *numbers
while initializing the array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是
numbers
不是常量指针,它是不可修改的左值
,因此您无法为其赋值。sizeof(int)
在任何特定实现上返回整数的大小sizeof(int*)
返回指向整数的指针的大小。sizeof()
的返回类型为size_t
(无符号)Yes
numbers
is not a constant pointer, it is a non modifiablelvalue
so you cannot assign to it.sizeof(int)
returns the size of an integer on any particular implementationsizeof(int*)
returns the size of a pointer to an integer.return type of
sizeof()
issize_t
(unsigned)sizeof(int)
返回用于存储 int 的字节数sizeof(int*)
返回用于存储指针的字节数声明初始化常量数组对于整数,您可以使用以下语法:
sizeof(int)
returns the number of bytes used to store an intsizeof(int*)
returns the number of bytes used to store a pointerTo declare an initialise a constant array of ints you can use the following syntax:
sizeof(int)
是数据类型的大小,sizeof(int*)
是指向数据类型的指针的大小。你不能将 p 分配给数字,因为数字被声明为基于固定长度堆栈的 int 数组,它不是 int 指针(尽管它可以转换为一个)
sizeof(int)
is the size of the data type,sizeof(int*)
is the size of a pointer to the datatype.you cannot assign p to numbers, because numbers is declared as a fixed length stack based int array, its not an int pointer(though it can be converted to one)
简单来说,
intnumbers[20];
是一个整数数组int * p;
是指向整数的指针; p 存储它指向的地址numbers = p;
是不可能的;两者的类型不同,一个是 int,另一个是 int*但是
numbers[0] = *p
;是可能的;提供 p 指向某个有效地址In simple words
int numbers [20];
is an integer arrayint * p;
is pointer to an integer; p stores the address it is pointing tonumbers = p;
is not possible ; both are of different type one is int and other is int*However
numbers[0] = *p
; is possible ; provided p points at some valid address