C语言 为什么我不能直接在 C 中将数组分配给指针?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3504506/
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
Why can't I assign an array to pointer directly in C?
提问by ant2009
I have the following program. However, I can't understand why I have to pass the address of the array. When they are both pointing to the same address. Which is the address of the first element of the array of int's.
我有以下程序。但是,我不明白为什么我必须传递数组的地址。当它们都指向同一个地址时。这是 int 数组的第一个元素的地址。
I get a warning when I try and do this "assignment from incompatible pointer type":
当我尝试执行此“从不兼容的指针类型进行分配”时收到警告:
ptr = var;
Complete source code:
完整的源代码:
void print_values(int (*ptr)[5])
{
size_t i = 0;
for(i = 0; i < 5; i++) {
printf("%d: [ %d ]\n", i, (*ptr)[i]);
}
}
int main(void)
{
/* declare a pointer to an array integers */
int (*ptr)[5] = NULL;
/* array of integers */
int var[] = {1, 2, 3, 4, 5};
/* assign the address of where the array is pointing to (first element) */
ptr = &var;
/* Both are pointing to the exact same address */
printf("var [ %p ]\n",(void*)var);
printf("&var [ %p ]\n", (void*)&var);
print_values(ptr);
return 0;
}
I compile the code with gcc 4.4.4 c89 -Wall -Wextra -O0
我编译代码 gcc 4.4.4 c89 -Wall -Wextra -O0
回答by CB Bailey
It's purely a type issue.
这纯粹是一个类型问题。
In most expression contexts the name of an array (such as var) decays to a pointer to the initial element of the array, not a pointer to the array. [Note that this doesn't imply that varis a pointer - it very much is nota pointer - it just behaveslike a pointer to the first element of the array in most expressions.]
在大多数表达式上下文中,数组的名称(例如var)衰减为指向数组初始元素的指针,而不是指向数组的指针。[请注意,这并不意味着它var是一个指针——它在很大程度上不是一个指针——它只是在大多数表达式中表现得像一个指向数组第一个元素的指针。]
This means that in an expression varnormally decays to a pointer to an int, not a pointer to an array of int.
这意味着在表达式中var通常会衰减为指向 的指针int,而不是指向 的数组的指针int。
As the operand of the address-of operator (&) is one context where this decay rule doesn't apply (the other one being as operand of the sizeofoperator). In this case the type of &varis derived directly from the type of varso the type is pointer to array of 5 int.
由于地址运算符 ( &)的操作数是一个不适用此衰减规则的上下文(另一个作为运算sizeof符的操作数)。在这种情况下,类型&var是直接从类型派生而来的,var所以类型是指向数组 5 的指针int。
Yes, the pointers have the same address value (the address of an arrays first element is the address of the array itself), but they have different types (int*vs int(*)[5]) so aren't compatible in the assignment.
是的,指针具有相同的地址值(数组第一个元素的地址是数组本身的地址),但它们具有不同的类型(int*vs int(*)[5]),因此在分配中不兼容。
ISO/IEC 9899:1999 6.3.2.1/4:
ISO/IEC 9899:1999 6.3.2.1/4:
Except when it is the operand of the
sizeofoperator or the unary&operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression of type "pointer to type" that points to the initial element of the array object and is not an lvalue. ...
除了当它是的操作数
sizeof操作者或一元&运算符,或者是用于初始化一个数组,其具有类型“的阵列的表达字符串文字类型”被转换为“输入指针”,它指向类型的表达式数组对象的初始元素,不是左值。...
回答by AnT
C is a strongly typed language. When a function expects a parameter of type int *, you have to pass an argument of type int *. Not double *, not char *, but int *. Even if the actual numerical address in those double *or char *is "the same" as the one you want to pass, it still doesn't change anything - you still have to pass an int *. The language prohibits you from passing the value of wrong type.
C 是一种强类型语言。当一个函数需要一个类型的参数时int *,你必须传递一个类型的参数int *。不是double *,不是char *,但是int *。即使这些中的实际数字地址double *或char *与您要传递的地址“相同”,它仍然不会改变任何内容-您仍然必须传递int *. 该语言禁止您传递错误类型的值。
This is exactly what happens in your case. The function takes a parameter of type int (*)[5]. That means that you have to pass the argument of that type. Passing an int *instead is not allowed. Whether the address is the same makes no difference.
这正是您的情况。该函数采用类型为 的参数int (*)[5]。这意味着您必须传递该类型的参数。int *不允许传递 an代替。地址是否相同没有区别。
回答by Ian Wetherbee
varitself is a (*int)pointing to the first element in your array. Pointers and arrays in C extremely similar. Change int (*ptr)[5] = NULL;to int* ptr = NULL;and ptr = &var;to ptr = var;
var本身是(*int)指向数组中第一个元素的指针。C 中的指针和数组极其相似。更改int (*ptr)[5] = NULL;到int* ptr = NULL;和ptr = &var;到ptr = var;
回答by Ultimate Gobblement
From what I can tell you are assigning an array pointer (var) to a pointer that points to an array pointer ((*ptr)[5]), so that's why you get that warning.
据我所知,您正在将一个数组指针 ( var)分配给一个指向数组指针 ( ) 的指针(*ptr)[5],所以这就是您收到该警告的原因。
Instead, try using
相反,尝试使用
int *ptr = NULL;

