标签:16px ++ class name 可见 print span -- alt
声明:
char name[4][10] = { "Justinian", "Momo", "Becky", "Bush" };
在内存中的存储:
J | u | s | t | i | n | i | a | n | \0 |
M | o | m | o | \0 | \0 | \0 | \0 | \0 | \0 |
B | e | c | k | y | \0 | \0 | \0 | \0 | \0 |
B | u | s | h | \0 | \0 | \0 | \0 | \0 | \0 |
这种方式会造成内存空间的浪费
声明:
char *name[4] = { "Justinian", "Momo", "Becky", "Bush" };
在内存中的存储:
name[0]
J | u | s | t | i | n | i | a | n | \0 |
name[1]
M | o | m | o | \0 |
name[2]
B | e | c | k | y | \0 |
name[3]
B | u | s | h | \0 |
可见此种方式能够避免不必要的内存浪费
验证结果:
#include <stdio.h> int main() { char name1[4][10] = { "Justinian", "Momo", "Becky", "Bush" }; char *name2[4] = { "Justinian", "Momo", "Becky", "Bush" }; printf("--------二维字符串数组的存储方式-------\n"); for(int i = 0; i < 4; i++) { printf("name[%d] = \"%s\"\t", i, name1[i]); printf("所占地址:%p\n", name1[i]); } printf("--------一维指针数组的存储方式--------\n"); for(int i = 0; i < 4; i++) { printf("name[%d] = \"%s\"\t", i, name2[i]); printf("所占地址:%p\n", name2[i]); } }
运行结果:
差距显而易见
标签:16px ++ class name 可见 print span -- alt
原文地址:https://www.cnblogs.com/0xWitch/p/9309318.html