gpt4 book ai didi

c - 如何从返回 C 中该字符串指针的函数中打印字符串?

转载 作者:太空狗 更新时间:2023-10-29 15:52:44 25 4
gpt4 key购买 nike

这是我的 C 程序:

#include <stdio.h>

char const* voice(void){
return "hey!";
}

int main(){
const char* (*pointer)(void);
pointer = &voice;


printf ("%s\n", *pointer); // check down *
return 0;
}
  • * 我正在尝试打印从指针返回的内容,但它似乎不起作用。

我做错了什么?

最佳答案

需要调用函数指针,即使用括号:

#include <stdio.h>

char const* voice(void){
return "hey!";
}

int main(){
const char* (*pointer)(void);
pointer = &voice;


printf ("%s\n", pointer());
// ^^^^^^^^^
return 0;
}

* 函数指针不需要。 (& 也不是)

关于c - 如何从返回 C 中该字符串指针的函数中打印字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15754665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com