作为我们在编程语言学院培训的一部分,我们也学习了C.在测试期间,我们遇到了程序输出的问题:
#include <stdio.h>
#include <string.h>
int main(){
char str[] = "hmmmm..";
const char * const ptr1[] = {"to be","or not to be","that is the question"};
char *ptr2 = "that is the qusetion";
(&ptr2)[3] = str;
strcpy(str,"(Hamlet)");
for (int i = 0; i < sizeof(ptr1)/sizeof(*ptr1); ++i){
printf("%s ", ptr1[i]);
}
printf("\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
后来,在检查了答案之后,很明显单元格(&ptr2)[3]与&ptr1 [2]中的存储单元相同,所以程序的输出是: to be or not to be (Hamlet)
我的问题是,是否有可能只通过笔记本中的书面代码,而不检查任何编译器,知道某个指针(或一般所有变量)跟随或先于内存中的其他变量?
注意,我不是指数组变量,因此数组中的所有元素必须按顺序排列.