How to assign function output to an array? In c?

I am trying to assign a function assignment to an array, but it does not seem to work when compiled.

The function takeInputsshould return an array. And, as I understand it, I thought it holdInputswas an array. However, it does not seem to compile. What is the mistake here?

// 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? Thank.

+4
source share
2 answers

The function takeInputsshould return an array. And, as I understand it, I thought it holdInputswas an array. However, it does not seem to compile. What is the mistake here?

The error in this explanation is that the function cannot return the array, as Jonathan Leffler pointed out in the comments.


accepts_Input.c: 53: 13: error: assignment of expression with array type 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);
+2

,

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

Source: https://habr.com/ru/post/1584693/


All Articles