C 字符数组和指针
#include<stdio.h>
int main(){
char a[6],*p;
a[0]='a';
a[1]='b';
a[2]='c';
a[3]='4';
a[4]='e';
a[5]='p';
a[6]='f';
a[7]='e';
printf("%s\n",a);
printf("printing address of each array element");
p=a;
printf("%u\n",&p[0]);
printf("%u\n",p+1);
printf("%u\n",a+2);
return 0;
}
输出如下...
anusha@anusha-laptop:~/Desktop/prep$ ./a.out
abc4epfe
printing address of each array element3216565606
3216565607
3216565608
当我将数组声明为 char a[6]
时,为什么它允许我在 a[7]
处分配一个值?是否不需要为最后一个元素附加空字符?
又 p=a => p 保存 char 数组 a 的第一个元素的地址。我不明白放置“&”是如何正确的地址前面 (p[0]
)。 &p[0]
表示 a
第一个元素的地址,这没有任何意义,至少对我来说是这样。
为什么它打印出正确的输出?
#include<stdio.h>
int main(){
char a[6],*p;
a[0]='a';
a[1]='b';
a[2]='c';
a[3]='4';
a[4]='e';
a[5]='p';
a[6]='f';
a[7]='e';
printf("%s\n",a);
printf("printing address of each array element");
p=a;
printf("%u\n",&p[0]);
printf("%u\n",p+1);
printf("%u\n",a+2);
return 0;
}
The output is as follows...
anusha@anusha-laptop:~/Desktop/prep$ ./a.out
abc4epfe
printing address of each array element3216565606
3216565607
3216565608
When I declared the array as char a[6]
why is it allowing me to allocate a value at a[7]
? Does it not need a null character to be appended for the last element?
Also p=a => p holds the address of first element of char array a. I don’t understand how it is correct to place an '&' in front of an address (p[0]
). &p[0]
means address of address of first element of a
which doesn't make any sense, at least to me.
Why is it printing the correct output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您刚刚调用了未定义的行为。推理超出数组范围的写入没有什么意义。只是不要这样做。
不,这是完全合理的。您的描述完美地描述了正在发生的事情。
&p[0]
与p
相同,而p
与a
相同。当您编写p[0]
时,您正在取消引用指针。然后,当您编写&p[0]
时,您将获取该变量的地址,从而返回到您开始的位置p
。You have just invoked undefined behaviour. There's little point in reasoning about writing beyond the bounds of an array. Just don't do it.
No, that's perfectly sensible. Your description perfectly describes what is going on.
&p[0]
is the same asp
which is the same asa
. When you writep[0]
you are dereferencing the pointer. When you then write&p[0]
you are taking the address of that variable and thus return to what you started from,p
.a
的最高有效索引是5
,因此您在数组边界之外写入了 2 个内容,是的,它仍然需要一个 NULL 终止符。它起作用的事实只是一个巧合。在数组边界之外写入是未定义的行为。它可以工作,它可能会让你的电脑崩溃,它可以用你的信用卡买披萨,或者完全不同的东西。你不知道它会做什么,所以就不要这样做。The highest valid index of
a
is5
, so you're writing outside the array bounds by two, and yes, it still needs a NULL terminator. The fact that it worked was just a coincidence; writing outside array bounds is undefined behaviour. It could work, it could crash your computer, it could go buy pizza with your credit card, or something entirely different. You have no idea what it will do, so just don't do it.因为 C 和 C++ 不关心,所以没有对数组进行边界检查。
不。例如,当你声明一个数组时,说 char a[7];你告诉编译器你需要七个字节,不多也不少。但是,如果您尝试访问数组外部,那就是您的问题了。
如果您编写
p[0]
,您将引用数组 p 的值,例如 ifint p[2] = {1,2};
thenp[ 0]
是 1如果你写
&p[0]
你会得到 p[0] 的地址,它基本上与p + 0
相同because C and C++ don't care, there is no bounds check on arrays.
no. e.g. when you declare an array say char a[7]; you tell the compiler you want seven bytes nothing more, nothing less. however if you try to access outside the array it is your problem.
if you write
p[0]
you are referencing the value of the array p e.g. ifint p[2] = {1,2};
thenp[0]
is 1if you write
&p[0]
you are getting the address of p[0] which is basically the same asp + 0
因为你告诉它这样做。你是老板。如果你告诉它跳下悬崖,它可能会跳下去。
它不是一个字符串,而是一个字符数组。除非您想将其视为字符串,否则它不需要在末尾加零。通过将其通过
%s
说明符传递给printf
,您将其视为字符串,因此您需要在末尾附加一个零,否则,您将通过需要字符串的格式说明符传递非字符串的内容。它的工作原理如下:
p
是指向第一个元素的指针。&p
是指针的地址。p[0]
是指针指向的数组中的第一个元素。&p[0]
是指针指向的数组中第一个元素的地址。纯粹是运气。最有可能的是,32 位(4 字节)的实现无法对 6 字节数组后面的两个字节做任何有用的事情。因此,它将其四舍五入为 8 字节,以便其后的下一个内容将从偶数 32 位边界开始。所以你使用了两个字节,但实现并没有用于任何用途。
Because you told it to do that. You're the boss. If you tell it to jump off a cliff, it might.
It's not a string, it's an array of characters. It does not need a zero at the end unless you want to treat it as a string. By passing it through a
%s
specifier toprintf
, you are treating it as a string, so you need to append a zero at the end, otherwise, you're passing something that's not a string through a format specifier that requires a string.It works like this:
p
is a pointer to the first element.&p
is the address of the pointer.p[0]
is the first element in the array the pointer points to.&p[0]
is the address of the first element in the array the pointer points to.Sheer luck. Most likely, the implementation, being 32-bits (4 bytes) couldn't do anything useful with the two bytes after the 6-byte array. So it rounded it up to 8 bytes so that the next thing after it would start at an even 32-bit boundary. So you used two bytes the implementation wasn't using for anything anyway.
它允许您在
a[7]
处分配一个值,因为您很幸运。这是未定义的行为。请参阅此处:http://ideone.com/ntjUn分段错误!
It allows you to allocate a value at
a[7]
because you're lucky. That's undefined behaviour. See here: http://ideone.com/ntjUnSegmentation fault!
我希望这对您理解数组和指针的关系有所帮助:
I hope this helps you a little with understanding the array&pointer relationship: