我目前正在观看有关 C 中指针的课程,并且我有一个关于多重间接的问题。我明白多重间接是什么,这没关系,但是我运行了一段代码,我试图以不同的方式做同样的事情,但它不起作用,我想知道为什么?我的问题是关于第 32 行的指针转换,为什么当我像这样转换它时这不起作用:printf("Value pointed to by gp is:%s\n",(char *)gp);
这是代码:
#include <stdio.h>
int data[3];
char *words[3];
int main(int argc, char **argv)
{
void *gp;
char *word = "rijec";
printf("%s\n",word);
for(int i = 0; i < 3;i++)
{
data[i] = i;
}
words[0] = "zero";
words[1] = "one";
words[2] = "two";
gp = data;
printf("\nAddress of array data is:%p\n",gp);
for(int i = 0; i < 3; i++)
{
printf("Value pointed to by gp is %d\n",*(int *)gp);
gp = (int*)gp+1;
}
gp=words;
printf("\nAddress of array of strings words is:%p\n",gp);
for(int i = 0;i < 3; i++)
{
printf("Value pointed to by gp is:%s\n",*(char **)gp);
gp = (char **)gp+1;
}
return 0;
}