徐建岗网络管理 2020-06-26
正确操作:
错误操作:编译时报错
double rate[] = {1.12, 1.22}; const double locked[] = {2.45, 55.3}; const double *pc = rate; /*可以*/ pc = locked; /*可以*/
const double locked[] = {2.45, 55.3}; double *pc = locked; /*不可以*/
/* * 1、STR1、STR2、str5都指向了同一个字符串地址; * 2、str3数组有自己单独的地址空间;* 3、str4数组在运行时为其分配空间并赋值,是静态存储区字符串的副本; * 4、格式化输入输出相同的字符串也只有一份; */ #include <stdio.h> #define STR1 "I am a student." #define STR2 "I am a student." char str3[] = "I am a student."; int main(void) { char str4[] = "I am a student."; const char *str5 = "I am a student."; printf("%p\n", STR1); printf("%p\n", STR2); printf("%p\n", str3); printf("%p\n", str4); printf("%p\n", str5); printf("the string size is:%u\n", sizeof(STR1)); printf("the string size is:%u\n", sizeof(STR2)); printf("the string size is:%d\n", sizeof(STR1)); return 0; }输出结果:0x1055c0x1055c0x210280x7eaeb1b40x1055cthe string size is:16the string size is:16the string size is:16
/* 部分数据段汇编代码 */ str3: .ascii "I am a student.\000" .section .rodata .align 2 .LC0: .ascii "I am a student.\000" .align 2 .LC1: .ascii "%p\012\000" .align 2 .LC2: .ascii "the string size is:%u\012\000" .align 2 .LC3: .ascii "the string size is:%d\012\000"