Trouble passing struct pointers in C to create a sequential list -


i have struct defined here

typedef struct { char name[10]; int idbadge; } employee; 

i want populate 1 instance of struct using function call:

void employeecall (char *name, int badgenumber, employee *e){     e = (employee *)malloc(sizeof(employee));     strcpy(e->name,name);     e->idbadge = badgenumber; } 

and then, want retrieve given piece of information using call this:

int employeebadge (employee e){     return(e.idbadge); } 

i call these functions main bellow:

int main(void){     employee a;     int badgenumber;      int badgenumbera = 1028;     char *nameptra = "fred";      employeecall( nameptra, badgenumbera, &a );      badgenumber = employeebadge(a);     printf("%d\n",badgenumber);      return 0; } 

when call them, compiles correctly however, return on employeebadge incorrect. returns int if nothing placed struct; badgenumber = 32767

how possible create struct variable can passed both value , reference? need employee variable pointer , assign employee employeecall function?

you code should not need allocate new employee, change employeecall to

void employeecall (char *name, int badgenumber, employee *e){      strcpy(e->name,name);     e->idbadge = badgenumber; } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -