gpt4 book ai didi

c - 如何在 c 中返回 char (*)[6]?

转载 作者:太空狗 更新时间:2023-10-29 15:17:40 24 4
gpt4 key购买 nike

我想按字母顺序对字符串数组进行排序,这是 c 中字符数组的数组。这是我的函数的主体:-

char (*)[6] sort_strings ( char (*sptr) [6])
{

//code.
//return a pointer of type char (*)[6].

}

但是编译器无法识别这种类型的返回类型。它给出错误提示:-

expected identifier or '(' before ')' token

那么我如何返回一个类型为 char (*)[6] 的指针呢?我还有一个问题,首先看 main() 如下:-

int main(){

char names[5][6] = {

"tom",
"joe",
"adam"
};

char (*result)[6] = sort_strings (names);

//code for printing the result goes here.

return 0;
}

所以我的下一个问题是,当我调用 sort strings (names) 时,编译器也会给我警告:-

initializing makes pointer from integer without a cast

所以我的问题是:-

1.如何从函数返回char(*)[6]?

2. 为什么我调用这个函数时编译器给我警告?

我在 Windows 的代码块上运行这段代码。

最佳答案

函数声明看起来像变量声明,只是变量名被函数名和参数代替。所以:

// asdf is a pointer to an array of 6 chars
char (*asdf)[6];

// sort_strings is a function returning a pointer to an array of 6 chars
// (and with an argument which is a pointer to an array of 6 chars)
char (*sort_strings ( char (*sptr)[6] )) [6];

关于c - 如何在 c 中返回 char (*)[6]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28643647/

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