Return from Struct in C

3

Hello, I am trying to return a struct from a function when it is called in main. I have done the whole function and define in the end two elements that are calculated in the function:

int mult[100];
char** elemfim = (char**)malloc(100 * sizeof(char*));

I want to put these two in a struct to give return to the main.

Then I defined at the beginning of the program like this:

struct elementodos{
    int multidor[100];
    char** cadaum;
};
elementodos interpretar(char* element);

But it generates an errp when I define my function, in part:

elementodos interpretar(char* element);

Should not I define my function like this? so I can put a struct return?

    
asked by anonymous 13.12.2017 / 12:29

1 answer

2

In this case, this is the signature of your role.

Put it there:

struct elementodos interpretar(char* element);

And in your role:

struct elementodos interpretar(char* element) { ... }

You can also use typedef to make it easier to build your code.

Ex:

struct elementodos{
    int multidor[100];
    char** cadaum;
};

typedef struct elementodos elem;

And then you can use it like this:

elem interpretar(char* element); e elem interpretar(char* element) { ... }

    
13.12.2017 / 12:46