Please see the below code:
#include <stdio.h>
#include <stdlib.h>
typedef int item_t;
typedef struct {item_t *base; item_t *top; int size;} stack_t;
stack_t *create_stack(int size)
{ stack_t *st;
/* Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?
*/
st = (stack_t *) malloc( sizeof(stack_t) );
/* How is this related to the above allocation of memory to st?
this code moves the pointer address to base of the stack. How does the
above syntax related to this?
*/
st->base = (item_t *) malloc( size * sizeof(item_t) );
st->size = size;
st->top = st->base;
return( st );
}
int main()
{ stack_t *st;
char nextop;
st = create_stack(50);
}
Please see the comment in the code where I have asked the question.
Is this just used to assign some memory to st pointer before using it? Is this allocation of memory necessary?
st = (stack_t *) malloc( sizeof(stack_t) );