这个program会print什么出来,可以解释一下简单的逻辑为什么会print那个单词吗
#include<stdio.h>void function (char **);
int main ()
{
char *arr[] = { "ant," "bat," "cat," "dog," "egg," "fly" };
function (arr);
return 0;
}
void function (char **ptr)
{
char *ptr1;
ptr1 = (ptr += sizeof (int));
printf ("%s\n", ptr1);
} 牛逼,考眼力的
"ant," "bat," "cat," "dog," "egg," "fly"这是1个字符串
首先,这个代码是跑不起来的,就不用考虑会pirnt什么
建议修改代码,让它跑起来再说
sh-5.1$ cat main.c
#include <stdio.h>
void function(char **ptr) {
char *ptr1;
//ptr1 = (ptr += sizeof(int));
ptr1 = &(*ptr += sizeof(int));
printf("%s\n", ptr1);
}
int main() {
char *arr[] = {"ant,""bat,""cat,""dog,""egg,""fly"};
function(arr);
return 0;
}
sh-5.1$ ./main
at,cat,dog,egg,fly
sh-5.1$
sh-5.1$ cat main.c
#include <stdio.h>
void function(char **ptr) {
char *ptr1;
ptr1 = (char *)&(ptr += sizeof(int));
printf("%s\n", ptr1);
}
int main() {
//char *arr[] = {"ant,""bat,""cat,""dog,""egg,""fly"};
char arr[] = {"ant,""bat,""cat,""dog,""egg,""fly"};
//function(arr);
function((char **)arr);
return 0;
}
sh-5.1$ ./main
fly
sh-5.1$
页:
[1]