如何将函数输出分配给数组类型?在C?

时间:2015-04-26 03:50:15

标签: c arrays function pointers

我尝试指定将函数的输出分配给数组,但是在编译时,它似乎不起作用。

函数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;
}

错误输出:

takes_Input.c: In function ‘main’:
takes_Input.c:53:13: error: assignment to expression with array type
  holdInputs = takeInputs(allInputs,numInputs);

但是我认为我将holdInputs初始化为数组?感谢。

2 个答案:

答案 0 :(得分:2)

  

函数takeInputs应该返回一个数组。而且,正如我所知,我认为holdInputs是一个数组。但是,它似乎没有编译。这里有什么错误?

这个解释中的错误是函数无法返回数组,正如Jonathan Leffler在评论中指出的那样。

  

takes_Input.c:53:13:错误:赋值给具有数组类型的表达式     holdInputs = takeInputs(allInputs,numInputs);

     

但是我认为我将holdInputs初始化为数组?

您确实将holdInputs声明为数组,并且它是一个数组。虽然你没有初始化它,但这应该不是问题。错误消息告诉您无法分配给阵列。例如,

char foo[4];
foo = "bar";        // This is an error
strcpy(foo, "bar"); // This is fine and dandy, like sour candy...

或者从代码中获取示例,您将在此处分配数组:

holdInputs = takeInputs(allInputs,numInputs);

也许你的意思是:

memcpy(holdInputs,takeInputs(allInputs,numInputs),numInputs*sizeof *allInputs);

答案 1 :(得分:1)

试试这个,

// 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;
}