OK, starting inside main() you have only 2 lines.
Code:
struct stack st;
initstack(&st );
The first one allocates space for a variable of type struct stack. The second one passes the address of your variable to the function initstack().
All you're doing is passing an address to a function. Here's the function definition:
Code:
void initstack(struct stack*st) // having trouble understanding this part.
{
st->top = 0;
}
All this does is take a pointer to a variable of type struct stack. A pointer just contains the address of a variable.
Therefore, the lone statement inside the function actually alters the struct that was passed to it because its address was passed. The statement above is equivalent to the following:
Because st inside this function is a pointer, you need to derefence it.
Forgetting structs for a moment, the pointer aspect of it is just like this:
Code:
void someFunction(int *);
int main(void)
{
int x;
someFunction(&x);
return 0;
}
void someFunction(int *p)
{
*p = 0;
}
If you realize the pointer relationship with regular variables, then it's just a matter of applying the concept over to structs.
Edit: Yes.... I'm getting slow in my old age.