Reputation: 13
I have a problem,the program gives an error like "expression must have pointer type".Can you help me ?
struct stack{
int i_data;
char c_data;
struct stack *next;
}top;
void push_i(struct top *newptr,int info){
newptr=(struct top*)malloc(sizeof(top));
if(newptr!=NULL){
top->c_data=NULL;
newptr->i_data=info;
newptr->next=*top;
*top=newptr;
}
Upvotes: 1
Views: 26797
Reputation: 91270
struct stack
with the variable top
top
variable isn't a pointer, you can't change what it points at. c_data
isn't a pointer, so don't assign NULL
to it. newptr
for anything useful - it should be a local variable,.This may work better:
struct stack{
int i_data;
char c_data;
struct stack *next;
};
...
struct stack * top = NULL;
...
void push_i(int info){
struct stack * newptr=(struct stack*)malloc(sizeof(struct stack));
if(newptr!=NULL){
top->c_data=0;
newptr->i_data=info;
newptr->next=top;
top=newptr;
}
Upvotes: 2
Reputation: 689
You have several problems. The 1st is you are using the type top
rather than the variable newptr
.
Also you might want to be using **newptr when passing the variable in.
Upvotes: 0
Reputation: 2531
Look at your 'struct stack', thats the description of the structure. Next loop at top, that's an instance of your structure. You appear to be mixing the both. There shouldn't be any 'struct top' anywhere, they should be 'struct stack'.
Upvotes: 0
Reputation: 36339
newptr->next = top
top = newptr
Remember, if x is declared T*, then x is the pointer, and *x is T. This is really not hard to understand. You want to assign the pointer, not overwrite where the pointer points.
Upvotes: 0