[英]Learning C - Pointers and memory addressing
I am learning C programming and I have a simple question about pointers... 我正在学习C编程,我有一个关于指针的简单问题......
I used the following code to play around with pointers: 我使用以下代码来玩指针:
#include <stdio.h>
int main (int argc, const char * argv[]) {
int * c;
printf("%x\n",c);
return 0;
}
When I print the value of C, I get back a 0. However, when I print &c (ie printf("&x\\n",&c) I get an address in memory... 当我打印C的值时,我返回0.但是,当我打印&c(即printf(“&x \\ n”,&c)时,我在内存中得到一个地址...
Shouldn't I be getting an address in memory when printing the pointer (ie printf("%x\\n",c)? 打印指针时,我不应该在内存中获取地址(即printf(“%x \\ n”,c)?
--- EDIT --- --- 编辑 ---
#include <stdio.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
char * a = malloc(11);
printf("String please\n");
scanf("%s",a);
printf("%s",a);
}
The question is, why does printf("%s",a) returns the string instead of the address that is stored in a? 问题是,为什么printf(“%s”,a)返回字符串而不是存储在?中的地址?
Shouldn't I use *a to follow the pointer and then print the string? 我不应该使用* a跟随指针然后打印字符串?
your current program is not correct. 您当前的程序不正确。 You define variable and do not set value before first use.
您在第一次使用之前定义变量并且不设置值。 the initial value is not guranteed for
c
, but you are lucky and it is equal to 0
. c
的初始值不保证,但你很幸运,它等于0
。 It means that c
points to nowhere. 这意味着
c
指向无处。 when you print &c
you print address of variable c
itself. 当你打印
&c
你打印变量c
本身的地址。 So actually both versions print address. 实际上两个版本都打印地址。
printf is actually quite a complex function and can be made to do all sorts of tricks by giving it the right format specifier. printf实际上是一个非常复杂的功能,可以通过为它提供正确的格式说明符来做各种技巧。
In your string example: 在你的字符串示例中:
printf("%s", a)
the "%s" tells the printf function that the following variable should be treated as a string. “%s”告诉printf函数应将以下变量视为字符串。 In C, a string is a pointer to one or more char, terminated by a char containing 0. This is a pretty common request, which is why printf supports a format specifier "%s" that triggers this relatively complex behavior.
在C中,字符串是指向一个或多个char的指针,由包含0的char终止。这是一个非常常见的请求,这就是printf支持触发这种相对复杂行为的格式说明符“%s”的原因。 If you want to print the address contained in the string pointer you have to use the format you found earlier:
如果要打印字符串指针中包含的地址,则必须使用之前找到的格式:
printf("%x\n",a);
which tells printf to treat the contents of a as an unsigned integer, and print it in base 16. 它告诉printf将a的内容视为无符号整数,并将其打印在base 16中。
*a would just be the value pointed to by a, which is just a single character. * a只是a指向的值,它只是一个字符。
You could print the single character with 你可以打印单个字符
printf("%c", *a);
Having int* c; 有int * c; If you print value of c, you get back a value that should be interpreted as a memory address of an integer value.
如果打印c的值,则返回一个应该被解释为整数值的内存地址的值。 In you example it might be 0 or something completely different as you are not initializing c.
在您的示例中,它可能是0或完全不同的东西,因为您没有初始化c。
If you print &c you get memory address of the pointer c (stored in stack). 如果你打印&c你得到指针c的内存地址(存储在堆栈中)。
#include <stdio.h>
int main (int argc, const char * argv[]) {
int * c;
int a=10;
c = &a;
printf("%x\n",c);
return 0;
}
This may clarify what happens when you make the int pointer point to something in memory. 这可能会澄清当你使int指针指向内存中的某些内容时会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.