-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
  • 186,650
  • 20
  • 189
  • 240
rohit singh
  • 41
  • 1
  • 5

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
  • 14,510
  • 27
  • 88
  • 115
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
  • 186,650
  • 20
  • 189
  • 240