在 C 中取消引用本地数组
dereference local array in C
根据我对 C 的理解,您可以将指针变量和数组变量视为等价的,因为它们最终都是指针(一个指向本地函数堆栈,另一个指向内存中的任意随机点)。
当我需要 return 一个指针时,我通常会传递一个指向指针的指针(例如 char ** pvar
),所以我可以看到将它传回一个取消引用的本地是多么没有意义数组,因为你不能改变变量的位置。
我的期望是,如果我尝试这样做,编译器会让我这样做,然后当我尝试设置 returning 指针值时出现段错误或崩溃。
但是,当尝试取消引用数组类型 (&array) 时,编译器会生成有关使用不兼容类型的警告,然后将指针传递给数组,从接收函数的查看。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
void ptrptr(uint32_t** dptr)
{
printf("%x, %x\n", dptr, *dptr);
}
void oneptr(uint32_t* ptr)
{
printf("%08x, %x\t", ptr, *ptr);
ptrptr(&ptr);
}
int main()
{
uint32_t array[] = {1};
uint32_t *ptr = calloc(1, sizeof( uint32_t));
ptr[0] = 3;
oneptr(ptr); /* OK, passes an (uint32_t *) */
oneptr(array); /* OK, passes an (uint32_t *) */
ptrptr(&ptr); /* OK, passes an (uint32_t **) */
ptrptr(&array); /* ??, passes an (uint32_t *) */
return 0;
}
编译它给我警告
cc test.c -o test
test.c: In function ‘main’:
test.c:24:9: warning: passing argument 1 of ‘ptrptr’ from incompatible pointer type [-Wincompatible-pointer-types]
ptrptr(&array);
^
test.c:5:6: note: expected ‘uint32_t ** {aka unsigned int **}’ but argument is of type ‘uint32_t (*)[1] {aka unsigned int (*)[1]}’
void ptrptr(uint32_t** dptr)
^~~~~~
0061a008, 3 7ebfa144, 61a008
7ebfa154, 1 7ebfa144, 7ebfa154
7ebfa150, 61a008
7ebfa154, 1
当我使用 gcc、clang 和 cl 编译它时得到相同的结果,所以我相当确信这不是编译器错误。那么问题是,当我尝试取消引用数组时,为什么 C 默默地传递一个指针 (uint32_t*
) 而不是指向一个指针 (uint32_t**
) 的指针?
The question then is, why is C silently passing a pointer (uint32_t*
) instead of a pointer to a pointer (uint32_t**
) when I try to dereference an array?
不是。
C 正在传递一个指向数组的指针 uint32_t (uint32_t(*)[1]
).
它是一个指向一个 uint32_t 数组的指针,因为它是一个 uint32_t 数组,你得到了一个指向它的指针。
不是沉默。您会收到一条编译器警告,提示 "hey, this is the wrong type of pointer!"。你以为这是什么?
test.c: In function ‘main’:
test.c:24:9: warning: passing argument 1 of ‘ptrptr’ from incompatible pointer type [-Wincompatible-pointer-types]
ptrptr(&array);
^
test.c:5:6: note: expected ‘uint32_t ** {aka unsigned int **}’ but argument is of type ‘uint32_t (*)[1] {aka unsigned int (*)[1]}’
void ptrptr(uint32_t** dptr)
您没有取消引用数组。您正在创建一个指向数组的指针,将其转换为错误的指针类型,然后取消引用它。
你给出数字 1 的原因是因为指向数组的指针实际上指向与指向数组中第一个事物的指针相同的地址。虽然,它是一种不同类型的指针,这意味着 ++
之类的东西工作方式不同,但随后您将其转换为相同类型的指针,因此您的代码不会注意到。
The question then is, why is C silently passing a pointer ( uint32_t*) instead of a pointer to a pointer (uint32_t**) when I try to dereference an array?
它不是无声的,它给了你一个警告。 C 标准没有提到术语 "errors" 和 "warnings",而是提到了诊断消息。为了遵循 C 标准,编译器向程序员显示诊断消息就足够了。
如果使用 gcc 或 clang 在违反 C 标准时想要错误而不是警告,则必须使用 -std=c11 -pedantic-errors
进行编译。
至于为什么代码不对,&array
以数组指针的形式给出了数组的地址,uint32_t(*)[1]
。此类型与 uint32_t**
不兼容。如果您 运行 程序尽管未指定 C 标准约束违规,但会发生什么:这是未定义的行为。没有保证 段错误或崩溃,这些只是未定义行为的许多潜在结果中的两个。
根据我对 C 的理解,您可以将指针变量和数组变量视为等价的,因为它们最终都是指针(一个指向本地函数堆栈,另一个指向内存中的任意随机点)。
当我需要 return 一个指针时,我通常会传递一个指向指针的指针(例如 char ** pvar
),所以我可以看到将它传回一个取消引用的本地是多么没有意义数组,因为你不能改变变量的位置。
我的期望是,如果我尝试这样做,编译器会让我这样做,然后当我尝试设置 returning 指针值时出现段错误或崩溃。
但是,当尝试取消引用数组类型 (&array) 时,编译器会生成有关使用不兼容类型的警告,然后将指针传递给数组,从接收函数的查看。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
void ptrptr(uint32_t** dptr)
{
printf("%x, %x\n", dptr, *dptr);
}
void oneptr(uint32_t* ptr)
{
printf("%08x, %x\t", ptr, *ptr);
ptrptr(&ptr);
}
int main()
{
uint32_t array[] = {1};
uint32_t *ptr = calloc(1, sizeof( uint32_t));
ptr[0] = 3;
oneptr(ptr); /* OK, passes an (uint32_t *) */
oneptr(array); /* OK, passes an (uint32_t *) */
ptrptr(&ptr); /* OK, passes an (uint32_t **) */
ptrptr(&array); /* ??, passes an (uint32_t *) */
return 0;
}
编译它给我警告
cc test.c -o test
test.c: In function ‘main’:
test.c:24:9: warning: passing argument 1 of ‘ptrptr’ from incompatible pointer type [-Wincompatible-pointer-types]
ptrptr(&array);
^
test.c:5:6: note: expected ‘uint32_t ** {aka unsigned int **}’ but argument is of type ‘uint32_t (*)[1] {aka unsigned int (*)[1]}’
void ptrptr(uint32_t** dptr)
^~~~~~
0061a008, 3 7ebfa144, 61a008
7ebfa154, 1 7ebfa144, 7ebfa154
7ebfa150, 61a008
7ebfa154, 1
当我使用 gcc、clang 和 cl 编译它时得到相同的结果,所以我相当确信这不是编译器错误。那么问题是,当我尝试取消引用数组时,为什么 C 默默地传递一个指针 (uint32_t*
) 而不是指向一个指针 (uint32_t**
) 的指针?
The question then is, why is C silently passing a pointer (
uint32_t*
) instead of a pointer to a pointer (uint32_t**
) when I try to dereference an array?
不是。
C 正在传递一个指向数组的指针 uint32_t (
uint32_t(*)[1]
).它是一个指向一个 uint32_t 数组的指针,因为它是一个 uint32_t 数组,你得到了一个指向它的指针。
不是沉默。您会收到一条编译器警告,提示 "hey, this is the wrong type of pointer!"。你以为这是什么?
test.c: In function ‘main’: test.c:24:9: warning: passing argument 1 of ‘ptrptr’ from incompatible pointer type [-Wincompatible-pointer-types] ptrptr(&array); ^ test.c:5:6: note: expected ‘uint32_t ** {aka unsigned int **}’ but argument is of type ‘uint32_t (*)[1] {aka unsigned int (*)[1]}’ void ptrptr(uint32_t** dptr)
您没有取消引用数组。您正在创建一个指向数组的指针,将其转换为错误的指针类型,然后取消引用它。
你给出数字 1 的原因是因为指向数组的指针实际上指向与指向数组中第一个事物的指针相同的地址。虽然,它是一种不同类型的指针,这意味着
++
之类的东西工作方式不同,但随后您将其转换为相同类型的指针,因此您的代码不会注意到。
The question then is, why is C silently passing a pointer ( uint32_t*) instead of a pointer to a pointer (uint32_t**) when I try to dereference an array?
它不是无声的,它给了你一个警告。 C 标准没有提到术语 "errors" 和 "warnings",而是提到了诊断消息。为了遵循 C 标准,编译器向程序员显示诊断消息就足够了。
如果使用 gcc 或 clang 在违反 C 标准时想要错误而不是警告,则必须使用 -std=c11 -pedantic-errors
进行编译。
至于为什么代码不对,&array
以数组指针的形式给出了数组的地址,uint32_t(*)[1]
。此类型与 uint32_t**
不兼容。如果您 运行 程序尽管未指定 C 标准约束违规,但会发生什么:这是未定义的行为。没有保证 段错误或崩溃,这些只是未定义行为的许多潜在结果中的两个。