Return of Struct in C

Asked

Viewed 751 times

3

Hello, I’m trying to return a struct from a function when it’s called on main. I did the whole function and set in the end two elements which are calculated in 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.

So I set 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);

Shouldn’t I set my function like this? so I can put a struct re-turn?

  • 1

    You forgot to say which error it generates.

  • Try it like this: struct elementodos interpretar(char* element);

  • Thank you Paulo Imon, that was the first part at least.

  • Oops! But in case this is just the signature of your function, you will have to change in function too.

  • I changed there too and it worked, puts as an answer to accept. Thank you very much.

  • Ready! Thanks! D

Show 1 more comment

1 answer

2


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

Put her like this:

struct elementodos interpretar(char* element);

And in your job:

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

You can also use the typedef to facilitate the construction of 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); and elem interpretar(char* element) { ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.