Deb*_*ish 5 c c++ arrays string pointers
#include <stdio.h>
#include <string.h>
int main() {
char *s[] = {"cricket","tennis","football"};
printf(" String are: \n\n");
printf(" %s \n", *(s));
printf(" %s \n", *(s+1));
printf(" %s \n", *(s+2));
printf(" \n\n");
printf(" Starting locations of the string are: \n\n");
printf(" %d\n",*(s));
printf(" %d\n",*(s+1));
printf(" %d\n",*(s+2));
printf(" \n\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
String are:
cricket
tennis
football
Starting locations of the string are:
134514112
134514120
134514127
Run Code Online (Sandbox Code Playgroud)
s是一个字符指针数组.s有三个元素,每个元素都存储字符串literal的起始地址.s [0]是一个指向"cricket"起始地址的指针.等等..
我的问题是:
通过观察这些地址,我们可以看到第二个字符串存储在第一个字符串的空字符之后.所有三个字符串都以顺序形式存储.这总是如此吗?
这是一个链接器决定 - 紧密存储或不存储字符串文字.没有保证.或者甚至可以通过编译器来完成 - 它可以创建包含所有相关文字的连续数据部分.但是,该部分的实际布局仍然是特定于实现的,您不应该假设它.