-2
struct node
{
    int val;
    struct node *left, *right;
};

// Stack type 

struct Stack
{
    int size;
    int top;
    struct node* *array;
};

struct Stack* createStack(int size)
{
    struct Stack* stack =
        (struct Stack*) malloc(sizeof(struct Stack));
    stack->size = size;
    stack->top = -1;
    stack->array =
        (struct node**) malloc(stack->size * sizeof(struct node*));
    return stack;
}

What does this statement do?

stack->array =
    (struct node**) malloc(stack->size * sizeof(struct node*));

What will be the memory representation of it?

dbush
  • 205,898
  • 23
  • 218
  • 273
rohit singh
  • 51
  • 1
  • 6

2 Answers2

0
stack->array =
 (struct node**) malloc(stack->size * sizeof(struct node*));

struct node** returns a pointer to a pointer (to the stack)

stack->size is the amount of items of the stack sizeof(struct node*) is the size of a pointer to a node.

So it creates an array of pointers, where each pointer points to one element within the stack.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

The above statement allocates space for an array of struct node *, i.e. pointers to struct node.

Each pointer in this array can then point to an instance of struct node.

dbush
  • 205,898
  • 23
  • 218
  • 273