C语言 C 指针和数组:[警告] 赋值使指针来自整数而不进行强制转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21858412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:47:01  来源:igfitidea点击:

C pointers and arrays: [Warning] assignment makes pointer from integer without a cast

carrayspointerswarnings

提问by user2274889

I'm having some trouble with pointers and arrays in C. Here's the code:

我在使用 C 中的指针和数组时遇到了一些麻烦。代码如下:

#include<stdio.h>


int *ap;
int a[5]={41,42,43,44,45};
int x;

int main()
{
    ap = a[4];
    x = *ap;
    printf("%d",x);
    return 0;
}

When I compile and run the code I get this warning:

当我编译并运行代码时,我收到此警告:

[Warning] assignment makes pointer from integer without a cast [enabled by default]

[警告] 赋值使指针从整数而不进行强制转换 [默认启用]

For line number 9 (ap = a[4];) and the terminal crashes. If I change line 9 to not include a position (ap = a;) I don't get any warnings and it works. Why is this happening? I feel like the answer is obvious but I just can't see it.

对于第 9 行(ap = a[4];),终端崩溃。如果我将第 9 行更改为不包含位置 (ap = a;),则不会收到任何警告并且可以正常工作。为什么会这样?我觉得答案很明显,但我就是看不到。

回答by Dipto

In this case a[4]is the 5thinteger in the array a, apis a pointer to integer, so you are assigning an integer to a pointer and that's the warning.
So apnow holds 45and when you try to de-reference it (by doing *ap) you are trying to access a memory at address 45, which is an invalid address, so your program crashes.

在这种情况下a[4]5th数组中的整数aap是指向整数的指针,因此您将整数分配给指针,这就是警告。
所以ap现在成立45,当您尝试取消引用它(通过执行*ap)时,您正在尝试访问地址 45 处的内存,这是一个无效地址,因此您的程序崩溃。

You should do ap = &(a[4]);or ap = a + 4;

你应该做ap = &(a[4]);ap = a + 4;

In carray names decays to pointer, so apoints to the 1st element of the array.
In this way, ais equivalent to &(a[0]).

c数组中,名称衰减为指针,因此a指向数组的第一个元素。
这样,a等价于&(a[0])

回答by halfbit

What are you doing: (I am using bytes instead of in for better reading)

你在做什么:(为了更好地阅读,我使用字节而不是 in)

You start with int *apand so on, so your (your computers) memory looks like this:

你从int *ap等等开始,所以你的(你的计算机)内存看起来像这样:

-------------- memory used by some one else --------
000: ?
001: ?
...
098: ?
099: ?
-------------- your memory  --------
100: something          <- here is *ap
101: 41                 <- here starts a[] 
102: 42
103: 43
104: 44
105: 45
106: something          <- here waits x

lets take a look waht happens when (print short cut for ...print("$d", ...)

让我们看看什么时候发生 (print short cut for ...print("$d", ...)

print a[0]  -> 41   //no surprise
print a     -> 101  // because a points to the start of the array
print *a    -> 41   // again the first element of array
print a+1   -> guess? 102
print *(a+1)    -> whats behind 102? 42 (we all love this number)

and so on, so a[0] is the same as *a, a[1] = *(a+1), ....

依此类推,所以 a[0] 与 *a 相同,a[1] = *(a+1), ....

a[n] just reads easier.

a[n] 只是读起来更容易。

now, what happens at line 9?

现在,第 9 行会发生什么?

ap=a[4] // we know a[4]=*(a+4) somehow *105 ==>  45 
// warning! converting int to pointer!
-------------- your memory  --------
100: 45         <- here is *ap now 45

x = *ap;   // wow ap is 45 -> where is 45 pointing to?
-------------- memory used by some one else --------
bang!      // dont touch neighbours garden

So the "warning" is not just a warning it's a severe error.

所以“警告”不仅仅是一个警告,它是一个严重的错误。

回答by njzk2

int[]and int*are represented the same way, except int[] allocates (IIRC).

int[]并且int*以相同的方式表示,除了 int[] 分配(IIRC)。

apis a pointer, therefore giving it the value of an integer is dangerous, as you have no idea what's at address 45.

ap是一个指针,因此给它一个整数值是危险的,因为你不知道地址 45 是什么。

when you try to access it (x = *ap), you try to access address 45, which causes the crash, as it probably is not a part of the memory you can access.

当您尝试访问它 ( x = *ap) 时,您尝试访问地址 45,这会导致崩溃,因为它可能不是您可以访问的内存的一部分。