C语言 如何将函数输出分配给数组类型?在 C?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29873504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign function output to an array type? In C?
提问by Sother
I try to assign to assign the output of a function to an array, but upon compiling, it doesn't seem to work.
我尝试分配将函数的输出分配给数组,但在编译时,它似乎不起作用。
The function takeInputsshould return an array. And, as I have it, I thought that holdInputsis an array. However, it doesn't seem to compile. What is the error here?
该函数takeInputs应该返回一个数组。而且,据我所知,我认为这holdInputs是一个数组。但是,它似乎无法编译。这里的错误是什么?
// Holds the expression that the user enters
struct Inputs
{
char word[10];
};
// Declare these:
struct Inputs* takeInputs(struct Inputs *userInputs, int numInputs); // must return a pointer to a pointer because returning array
struct Inputs* printInputs(struct Inputs *userInputs);
struct Inputs* takeInputs(struct Inputs *userInputs,int numInputs){
/*Inputs:
userInputs: an array of struct Inputs, each of which contain a string called "word"
numInputs: integer from user
*/
int i;
for (i=0;i<numInputs;i++){
printf("please input the word");
fgets(userInputs[i].word,10,stdin);
}
}
int main(int argc, char const *argv[]){
//user Input should look like this: ./takes_Input.exe 7
if (argc!=2){
error("user Input should look like this: ./takes_Input.exe 7");
}
// represents the number of words the user is entering
int numInputs = atoi(argv[2]);
struct Inputs allInputs[numInputs];
struct Inputs holdInputs[numInputs];
holdInputs = takeInputs(allInputs,numInputs);
// printInputs(holdInputs);
return 0;
}
Error output:
错误输出:
takes_Input.c: In function ‘main':
takes_Input.c:53:13: error: assignment to expression with array type
holdInputs = takeInputs(allInputs,numInputs);
But I thought I initialized holdInputs as an array?? Thanks.
但我以为我将holdInputs 初始化为一个数组??谢谢。
采纳答案by autistic
The function
takeInputsshould return an array. And, as I have it, I thought thatholdInputsis an array. However, it doesn't seem to compile. What is the error here?
该函数
takeInputs应该返回一个数组。而且,据我所知,我认为这holdInputs是一个数组。但是,它似乎无法编译。这里的错误是什么?
The error in this explanation is that a function can't return an array, as Jonathan Leffler pointed out in the comments.
这个解释中的错误是函数不能返回数组,正如 Jonathan Leffler 在评论中指出的那样。
takes_Input.c:53:13: error: assignment to expression with array type holdInputs = takeInputs(allInputs,numInputs);
But I thought I initialized holdInputs as an array??
take_Input.c:53:13: 错误:赋值给数组类型为holdInputs = takeInputs(allInputs,numInputs)的表达式;
但我以为我将holdInputs 初始化为一个数组??
You did indeed declare holdInputsas an array, and it is an array. Though you haven't initialised it, that shouldn't be a problem. The error message is telling you that you can't assign to an array. For example,
你确实声明holdInputs为一个数组,它是一个数组。虽然你还没有初始化它,但这应该不是问题。错误消息告诉您无法分配给数组。例如,
char foo[4];
foo = "bar"; // This is an error
strcpy(foo, "bar"); // This is fine and dandy, like sour candy...
Or to take an example from your code, you're assigning to the array here:
或者以您的代码为例,您在此处分配给数组:
holdInputs = takeInputs(allInputs,numInputs);
Perhaps you meant:
也许你的意思是:
memcpy(holdInputs,takeInputs(allInputs,numInputs),numInputs*sizeof *allInputs);
回答by Andre
Try this,
尝试这个,
// Holds the expression that the user enters
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct Inputs { char word[10]; };
// Declare these:
__typeof__(struct Inputs (*)[]) takeInputs(struct Inputs (*userInputs)[],
int numInputs); // must return a pointer to a pointer because returning array
void printInputs(struct Inputs (*userInputs)[]);
void printInputs(struct Inputs (*userInputs)[]){
struct Inputs * ptr = *userInputs;
for(;*ptr->word != 0; ptr++){
printf("%s", ptr->word);
}
}
__typeof__(struct Inputs (*)[]) takeInputs(struct Inputs (*userInputs)[], int numInputs){
/*Inputs:
userInputs: an array of struct Inputs, each of which contain a string called "word"
numInputs: integer from user
*/
int i;
for (i=0;i<numInputs;i++){
struct Inputs * ptr = (*userInputs+i);
printf("please input the word\t");
fgets(ptr->word, 10, stdin);
}
return userInputs;
}
int main(int argc, char const *argv[]){
//user Input should look like this: ./takes_Input.exe 7
//if (argc!=2){
// printf("user Input should look like this: ./takes_Input.exe 7");
// exit(1);
//}
// represents the number of words the user is entering
//const char num[] = {4};
int numInputs = 7;//atoi(num);//atoi(argv[2]);
struct Inputs allInputs[numInputs];
/* this array pointer points to allInputs[] */
struct Inputs (*holdInputs)[numInputs];
holdInputs = takeInputs(&allInputs, numInputs);
printInputs(holdInputs);
return 0;
}

