C 字符数组和指针

发布于 2024-12-15 11:01:54 字数 751 浏览 1 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一腔孤↑勇 2024-12-22 11:01:55

您刚刚调用了未定义的行为。推理超出数组范围的写入没有什么意义。只是不要这样做。

&p[0] 表示 a[] 第一个元素的地址,这是不合理的

不,这是完全合理的。您的描述完美地描述了正在发生的事情。 &p[0]p 相同,而 pa 相同。当您编写 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.

&p[0] means address of address of first element of a[] which is not sensible

No, that's perfectly sensible. Your description perfectly describes what is going on. &p[0] is the same as p which is the same as a. When you write p[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.

開玄 2024-12-22 11:01:55

a 的最高有效索引是 5,因此您在数组边界之外写入了 2 个内容,是的,它仍然需要一个 NULL 终止符。它起作用的事实只是一个巧合。在数组边界之外写入是未定义的行为。它可以工作,它可能会让你的电脑崩溃,它可以用你的信用卡买披萨,或者完全不同的东西。你不知道它会做什么,所以就不要这样做。

The highest valid index of a is 5, 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.

庆幸我还是我 2024-12-22 11:01:55

当我将数组声明为 char a[6] 时,为什么它允许我在 a[7] 处分配值?

因为 C 和 C++ 不关心,所以没有对数组进行边界检查。

最后一个元素不需要添加空字符吗?

不。例如,当你声明一个数组时,说 char a[7];你告诉编译器你需要七个字节,不多也不少。但是,如果您尝试访问数组外部,那就是您的问题了。

我不明白标记“&”是如何正确的前面一个
地址(由p[0]标记)。 '&p[0]' 表示第一个地址的地址
a[] 的元素不合理,对吧?为什么它会打印
正确的输出?

如果您编写 p[0],您将引用数组 p 的值,例如 if int p[2] = {1,2}; then p[ 0] 是 1
如果你写 &p[0] 你会得到 p[0] 的地址,它基本上与 p + 0 相同

when i declared the array as char a[6] why is it allowing me to allocate a value at a[7]?

because C and C++ don't care, there is no bounds check on arrays.

Does it not need a null character to be appended for the last element?

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.

I donot understand how it is correct to mark an '&' infront of an
address(marked by p[0]). '&p[0]' means address of address of first
element of a[] which is not sensible right?How come it is printing
correct output?

if you write p[0] you are referencing the value of the array p e.g. if int p[2] = {1,2}; then p[0] is 1
if you write &p[0] you are getting the address of p[0] which is basically the same as p + 0

廻憶裏菂餘溫 2024-12-22 11:01:55

当我将数组声明为 char a[6] 时,为什么它允许我在 a[7] 处分配值?

因为你告诉它这样做。你是老板。如果你告诉它跳下悬崖,它可能会跳下去。

不需要为最后一个元素附加空字符吗?

它不是一个字符串,而是一个字符数组。除非您想将其视为字符串,否则它不需要在末尾加零。通过将其通过 %s 说明符传递给 printf,您将其视为字符串,因此需要在末尾附加一个零,否则,您将通过需要字符串的格式说明符传递非字符串的内容。

还有 p=a => p 保存 char 数组 a 的第一个元素的地址。我不明白放置“&”是如何正确的在地址(p[0])前面。 &p[0] 表示 a 的第一个元素的地址的地址,至少对我来说没有任何意义。

它的工作原理如下:
p 是指向第一个元素的指针。
&p 是指针的地址。
p[0] 是指针指向的数组中的第一个元素。
&p[0] 是指针指向的数组中第一个元素的地址。

为什么打印出正确的输出?

纯粹是运气。最有可能的是,32 位(4 字节)的实现无法对 6 字节数组后面的两个字节做任何有用的事情。因此,它将其四舍五入为 8 字节,以便其后的下一个内容将从偶数 32 位边界开始。所以你使用了两个字节,但实现并没有用于任何用途。

When I declared the array as char a[6] why is it allowing me to allocate a value at a[7]?

Because you told it to do that. You're the boss. If you tell it to jump off a cliff, it might.

Does it not need a null character to be appended for the last element?

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 to printf, 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.

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.

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.

Why is it printing the correct output?

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.

九歌凝 2024-12-22 11:01:55

它允许您在 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/ntjUn

Segmentation fault!

梦里南柯 2024-12-22 11:01:55

我希望这对您理解数组和指针的关系有所帮助:

a[i] == *(a+i) == *(i+a) == i[a]

I hope this helps you a little with understanding the array&pointer relationship:

a[i] == *(a+i) == *(i+a) == i[a]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文