Code:
void push (StackPtr *top, char p)
{
StackPtr newptr;
newptr = malloc();
newptr.elem = p;
newptr->next = top;
top = newptr;
}
This is incorrect. Close, but incorrect. I'll assume you're using pseudo code, in which, this would be correct. It, correctly, would be this:
Code:
void push (StackPtr *top, char p)
{
StackPtr newptr;
newptr = malloc( sizeof( StackType ) );
newPtr->elem = p; /* you're using a pointer, remember */
newptr->next = top;
top = newptr;
}
Yeah. Your second example is correct.
using 'typedef' means you're defining a new variable type. I actually prefer to do this when making my nodes:
Code:
typedef struct node Node;
Node {
Node *n;
int myVar;
};
Nice and clean.
Quzah.