Reputation: 1975
I have this struct :
typedef struct std_fifo{
char* name;
struct std_fifo* next;
}std_fifo, *fifo;
And the two following methods :
char* remove(fifo* f){
if((*f)==NULL)
return NULL;
char* tmp=(*f)->name;
fifo tmpFifo= *f;
*f=(*f)->next;
free(tmpFifo);
return tmp;
}
fifo add(fifo f,char* new){
if(f==NULL){
fifo newFifo=malloc(sizeof(std_fifo));
newFifo->name=malloc(strlen(new)+1);
strcpy(newFifo->name,new);
newFifo->next=NULL;
return newFifo;
}
f->next=add(f->next,new);
return f;
}
I would like to rewrite this, but without the typedef fifo*. I mean with this struc :
struct Fifo{
char* name;
struct Fifo* next;
};
My question may seem ridiculous but I have difficulities with struct and typedef, so it would be really nice if someone could help me.
Thanks a lot.
EDIT:
I did something like that :
char* remove(struct Fifo* fifo){
if(fifo == NULL)
return NULL;
char* name = fifo->name;
fifo = fifo->next;
return name;
}
Is it correct ?
Upvotes: 0
Views: 244
Reputation: 7198
struct
defines a NOT trivial type (int, char, etc are trivial types), containing one or more fields. typedef
defines an alias, a short version of a long definition. Despite of the world being full of trivial typedefs, such use is not advised; better reserve it for not trivial, long definitions.
For example, let's define a pointer to an unsigned int, and then a var of that type:
typedef unsigned int* uip;
uip myVar; //myVar is a pointer to an unsigned int
Using a typedef struct
combination may be possible (?). Anyhow is not needed because struct is another word used for definintions.
struct myType
{
int val;
char* array;
/*...*/
}
myType myStruct; //myStruct is of type myType
Upvotes: 0
Reputation: 137527
You have to use struct Fifo
everywhere. The struct
token is part of its name.
For example:
char* remove(struct Fifo* f) {
Upvotes: 1