内存分配数据不对
#include <stdio.h>
int main() {
char str[] = "hello";
char *str2 = "world";
char str3[10];
printf("input a value\n");
scanf("%s", str);
//str[3] = '\0';
printf("str is %s\n", str);
printf("str2 is %s\n", str2);
printf("str3 is %s\n", str3);
return 0;
}
下面是调试过程:
Temporary breakpoint 1, main () at main.c:3
3 int main() {
(gdb) n
5 char str[] = "hello";
(gdb)
6 char *str2 = "world";
(gdb)
10 printf("input a value\n");
(gdb)
input a value
11 scanf("%s", str);
(gdb)
aaaaaaaaaaaaaaaaaaaa
14 printf("str is %s\n", str);
(gdb) p &str
$1 = (char (*)[6]) 0x7fffffffdb00
(gdb) p &str2
$2 = (char **) 0x7fffffffdaf8
(gdb) p &str3
$3 = (char (*)[10]) 0x7fffffffdb10
(gdb) p strt
No symbol "strt" in current context.
(gdb) p str
$4 = "aaaaaa"
(gdb) p str3
$5 = "aaaa\000\000\000\000P\005"
str 是6个字节,无论我怎么输入,str打印出来的都是6个字节,并不是实际长度,还有就是为什 str 和 str3 的内存之间相差的是16个字节,而不是6个呢,str 的地址是 0x7fffffffdb00 , 而 str3 的地址是 0x7fffffffdb10