C++ C指针地址打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21104899/
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
C pointer address printing
提问by Robert Rocha
I am learning about C pointers and addresses for the first time and how to use them on my tablet
我第一次学习 C 指针和地址以及如何在我的平板电脑上使用它们
Let's say:
让我们说:
int x = 1, y = 2;
int *ip; // declares *ip as an int type?
ip = &x; //assigns the address of x to ip?
y = *ip; //y is now 1 via dereferencing
Are all the comment explanations correct?
所有的评论解释都正确吗?
What happens if I print the result of ip? Will it print the address of variable x, something like
如果我打印 ip 的结果会发生什么?它会打印变量 x 的地址,比如
011001110
011001110
回答by haccks
Yes. All of your statements are correct. However in case of first
是的。你的所有陈述都是正确的。但是在第一个的情况下
int *ip;
it is better to say that ip
is a pointer to an int
type.
最好说它ip
是一个int
类型的指针。
What happens if I print the result of ip?
如果我打印 ip 的结果会发生什么?
It will print the address of x
.
它将打印 的地址x
。
Will it print the address of variable x, something like
011001110
它会打印变量 x 的地址,比如
011001110
No. Addresses are generally represented in hexadecimal. You should use %p
specifier to print the address.
否。地址通常以十六进制表示。您应该使用%p
说明符来打印地址。
printf("Address of x is %p\n", (void *)ip);
NOTE:
Note that in the above declaration *
is not the indirection operator. Instead it specify the type of p
, telling the compiler that p
is a pointer to int
. The *
symbol performs indirection only when it appears in a statement.
注意:
请注意,在上面的声明*
中不是间接运算符。相反,它指定 的类型p
,告诉编译器这p
是一个指向 的指针int
。该*
符号执行,只有当它出现在一份声明中间接。
回答by Andrew L
int x = 1, y = 2;
int *ip; // declares ip as a pointer to an int (holds an address of an int)
ip = &x; // ip now holds the address of x
y = *ip; // y now equals the value held at the address in ip
回答by Fiddling Bits
Consider the following as an example:
以以下为例:
Initializer x y ip Memory Value [1] [2] [1000] Memory Address 1000 1004 1008
As you can see:
如你看到的:
x
has the value1
and the address1000
y
has the value2
and the address1004
ip
has the value1000
(the address ofx
) and the address1008
x
有值1
和地址1000
y
有值2
和地址1004
ip
具有值1000
(的地址x
)和地址1008
Consider the following:
考虑以下:
x == 1
and&x == 1000
y == 2
and&y == 1004
ip == 1000
and&ip == 1008
and*ip == 1
(the value ofx
)
x == 1
和&x == 1000
y == 2
和&y == 1004
ip == 1000
和&ip == 1008
和*ip == 1
(的值x
)
Hope this helps you visualize what's happening.
希望这可以帮助您想象正在发生的事情。
回答by adrian
It's all correct.
都是对的。
1st line: you declare two variables
第一行:声明两个变量
2nd line: a memory pointer "ip" is defined
第二行:定义了内存指针“ip”
3rd line: The memory adress of X is given to the pointer ip
第 3 行:将 X 的内存地址赋予指针 ip
4th line: Y is now set to the value of the variable X at the address ip
第 4 行:Y 现在设置为地址 ip 处的变量 X 的值
The memory address, however, is in hexadecimal format. 011001110 is a byte of data and not a memory address. The address is more likely going to be something like 0x000000000022FE38 (it may be shorter). Consider this:
但是,内存地址是十六进制格式。011001110 是一个字节的数据,而不是一个内存地址。该地址更有可能类似于 0x000000000022FE38(可能更短)。考虑一下:
int x = 0;
int *ptr = &x; //the "&" character means "the address of"
printf("The variable X is at 0x%p and has the value of %i", (void *)ptr, x);
This would print the address of X, rather than *ptr. You'd need another pointer to print the address of *ptr. But that's rather pointless, as a pointer is defined to print the address of a variable. Think of it as an alias for another variable (it's value is the value at that memory address).
这将打印 X 的地址,而不是 *ptr。您需要另一个指针来打印 *ptr 的地址。但这毫无意义,因为定义了一个指针来打印变量的地址。将其视为另一个变量的别名(它的值是该内存地址处的值)。