Ç - (PTR = =放大器; PTR)什么是* PTR? [英] C - (ptr = = &ptr) What is *ptr?
问题描述
这可能是一个愚蠢的问题,但我有一个小问题,用C指针的理解。更当谈到阵列。例如:
This might be a stupid question, but I have a little problem with understanding of C Pointers. Even more when it comes to arrays. For example:
char ptr[100];
ptr[0]=10;
fprintf(stderr, "&ptr: %p \n ptr: %p \n*ptr: %d\n", &ptr, ptr, *ptr);
if ( &ptr == ptr ) {
fprintf(stderr, "Why?\n");
}
这怎么可能? PTR'是在ADRESS&放大器; PTR。和PTR的内容是一样的&放大器; PTR。那么为什么是* PTR = 10 ???
How is this even possible? 'ptr' is at the adress &ptr. And the content of ptr is the same as &ptr. Then why is *ptr = 10 ???
推荐答案
数组的第一元素的地址是相同的数组本身的地址。
The address of the first element of the array is the same as the address of the array itself.
除了当它的操作数的的sizeof
或地址的&安培;
运营商,或者是一个字符串文字被用来初始化声明另一个阵列,将被隐式转换(衰变)型T N个元素的数组的前pression键入指针T和值将是地址阵列中的第一个元素。
Except when it is the operand of the sizeof
or address-of &
operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be implicitly converted ("decay") to type "pointer to T" and the value will be the address of the first element in the array.
如果除权pression A
的类型是T N个元素的数组,那么前pression &放;一个
是键入指针至T N个元素的数组,或 T(*)[N]
。
If the expression a
is of type "N-element array of T", then the expression &a
is type "pointer to N-element array of T", or T (*)[N]
.
由于声明
T a[N];
那么下面的都是真实的:
then the following are all true:
Expression Type Decays to
---------- ---- ---------
a T [N] T *
&a T (*)[N] n/a
*a T n/a
这位前pressions A
和&放大器;一个
都计算出相同的值的(阵列中的第一个元素的位置),但有不同的类型(分别为指针T和指针至T的阵列)。
The expressions a
and &a
both evaluate to the same value (the location of the first element in the array), but have different types (pointer to T and pointer to array of T, respectively).
这篇关于Ç - (PTR = =放大器; PTR)什么是* PTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!