简体   繁体   English

玩指针,char *字符串使人迷茫和困惑

[英]Playing with pointers, dazed and confused by char * strings

So i was playing with pointers to understand different use cases, I'm not so experienced at this and unable to wrap my head around some ideas. 因此,我在玩指针以了解不同的用例,对此我并不那么有经验,也无法将自己的想法付诸实践。 I'll mark the lines where i have a problem. 我会在出现问题的地方划上界线。 Please help me understand pointers. 请帮助我理解指针。

on line 6: 在第6行:

Why can't I refer a string as an array, since that's how it's stored in memory i think. 我为什么不能将字符串称为数组,因为我认为这就是它存储在内存中的方式。 How to access individual characters of a string? 如何访问字符串的各个字符?

on line 7-8: 在7-8行:

why is there a difference of 8 bytes between &str and &str + 1 when int datatype takes 4-bytes int数据类型占用4个字节时,为什么&str&str + 1之间有8个字节的差异

on line 9,10 and 12: 在第9,10和12行:

Why does using *str to refer to the string take int as input? 为什么使用*str引用字符串将int作为输入? and why does it cast a single byte char to int instead of casting 4 bytes? 为什么将单个字节的charint而不是转换4个字节?

char *str = "Ninechars";
printf("Start\n");
printf("%s\n", str);
printf("%p\n", str);    // address of first char of str
printf("%p\n", str+1);  // address of second char of str
// printf("%s\n", str[1]);  // [segfault] warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [TODO]
printf("%p\n", &str);   // address to head of the string? NO, address of the pointer var str
printf("%p\n", &str+1); // [TODO], difference of 8bytes b/w this and previous address value? [WHY]?
printf("%d\n", *str);   // what integer is this? -> whatever can be made from 'N'=78
printf("%d\n", *(str+1));   // 'i'=105, but why is it taking single byte ints [TODO]
printf("%ld\n", sizeof(1)); // 4 bytes, as expected
// printf("%s\n", *str);    // [?]Wrong, [HOW][WHY], format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’[TODO]
printf("End\n");
Output:
Start
Ninechars
0x40088f
0x400890
0x7fff95a594e8
0x7fff95a594f0
78
105
4
End

The full code file is here: http://ideone.com/ryag80 完整的代码文件在这里: http : //ideone.com/ryag80

Let's examine it line by line: 让我们逐行检查它:


char *str = "Ninechars";
printf("Start\n");
printf("%s\n", str);
printf("%p\n", str);    // address of first char of str
printf("%p\n", str+1);  // address of second char of str

The above addresses are part of the data segment of process' virtual memory. 以上地址是进程虚拟内存的数据段的一部分。


printf("%s\n", str[1]);

Actually, str[1] == *(str + 1) . 实际上, str[1] == *(str + 1) What %s expects though is a char * , thus (str + 1) or &str[1] . %s期望的是char * ,即(str + 1)&str[1]


printf("%p\n", &str);   
printf("%p\n", &str+1);

&str is the address of str , a pointer to char . &str是的地址str ,一个指针到char It's on the stack. 它在堆栈上。

The difference of 8 bytes means that, on your system , the size of a pointer to char is 8 bytes. 8个字节的差异意味着, 在您的系统上 ,指向char的指针的大小为8个字节。 Try printf("\\nsizeof(char*) == %zd\\n", sizeof(char*)) to verify it. 尝试printf("\\nsizeof(char*) == %zd\\n", sizeof(char*))进行验证。


printf("%d\n", *str);

It's ASCII code for N . 它是NASCII码


printf("%d\n", *(str+1));

Indeed 'i' == 105 ( ASCII code again), and you instructed printf() to print it as an int using %d . 确实是'i' == 105 (再次是ASCII码 ),并且您指示printf()使用%d将其打印为int


printf("%ld\n", sizeof(1)); // 4 bytes, as expected
printf("%s\n", *str);    // [?]Wrong, [HOW][WHY], format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’[TODO]

Again, using %s , printf() expects a string ( char * in C), but *str is actually a char (an 8-byte int ). 同样,使用%sprintf()需要一个字符串(C中的char * ),但是*str实际上是一个char (一个8字节的int )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM