Victorziyou 发表于 2023-3-8 13:16:09

这个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);
}

jhq999 发表于 2023-3-9 15:21:36

牛逼,考眼力的
"ant," "bat," "cat," "dog," "egg," "fly"这是1个字符串

cjgank 发表于 2023-3-10 17:51:59

首先,这个代码是跑不起来的,就不用考虑会pirnt什么

建议修改代码,让它跑起来再说

人造人 发表于 2023-3-10 20:20:13

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$

人造人 发表于 2023-3-10 20:31:30

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]
查看完整版本: 这个program会print什么出来,可以解释一下简单的逻辑为什么会print那个单词吗