且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C指针和数组:【警告】赋值时将整数指针,未作投

更新时间:2022-06-27 00:50:34

在这种情况下, A [4] 4 数组中的整数 A AP 是整数的指针,所以你分配给一个整数指针,这就是警告。照片所以 AP 现在持有 45 ,当你试图去参考它(通过执行 * AP )你想在地址45,这是一个无效的地址来访问内存,所以你的程序崩溃。

In this case a[4] is the 4th integer in the array a, ap is a pointer to integer, so you are assigning an integer to a pointer and that's the warning.
So ap now holds 45 and 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.

您应该做的 AP =放大器(A [4]); AP = A + 4;

C 数组名衰变为指针,所以 A 指向数组的第一个元素。
这样, A 等同于&功放;(A [0])

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