Question: I'm studying Data Structures, and I'm not getting why stacks and queues need to be declared like:

```
struct stack *Stack;

```

(forget about the `struct` syntax)

I mean, why it is always declared as a pointer?

Answer: 
They are not always declared like that!

In general, declaring a variable as a pointer is useful for later allocating it dynamically. This can be due to a couple of reasons:

* The variable is too big for the program stack
* You want to return that variable from a function

In your case, let's think of two different implementations of stack:

```
struct stack
{
    void *stuff[10000];
    int size;
};

```

This is a terrible implementation, but assuming you have one like this, then you'd most probably not want to put it on the program stack.

Alternatively, if you have:

```
struct stack
{
    void **stuff;
    int size;
    int mem_size;
};

```

You dynamically change the size of `stuff` anyway, so there is absolutely no harm in declaring a variable of type `struct stack` on the program stack, i.e. like this:

```
struct stack stack;

```

Unless, you'd want to return it from a function. For example:

```
struct stack *make_stack(int initial_size)
{
    struct stack *s;

    s = malloc(sizeof(*s));
    if (s == NULL)
        goto exit_no_mem;

    if (initial_size == 0)
        initial_size = 1;
    s->stuff = malloc(initial_size * sizeof(*s->stuff));
    if (s->stuff == NULL)
        goto exit_no_stuff_mem;

    s->size = 0;
    s->mem_size = initial_size;

    return s;
exit_no_stuff_mem:
    free(s);
exit_no_mem:
    return NULL;
}

```

Personally, though, I would have declared the function like this:

```
int make_stack(struct stack *s, int initial_size);

```

and allocate the `struct stack` on the program stack.
It depends on how your stack structure is defined (not just the layout of the `struct`, but the operations that manipulate it as well). 

It's entirely possible to define a stack as a simple array and index, such as

```
struct stack_ {
  T data[N]; // for some type T and size N
  size_t stackptr; // Nobody caught that error, so it never existed, right? ;-)
} stack;

stack.stackptr = N; // stack grows towards 0

// push operation
if (stack.stackptr)
  stack.data[--stack.stackptr] = some_data();
else
  // overflow

// pop operation
if (stack.stackptr < N)
  x = stack.data[stack.stackptr++];
else
  // underflow

```

However, fixed-sized arrays are limiting. One easy method of implementing a stack is to use a list structure:

```
struct stack_elem {
  T data;
  struct stack_elem *next;
};

```

The idea is that the head of the list is the top of the stack. Pushing an item onto the stack adds an element at the head of the list; popping an item removes that element from the head of the list:

```
int push(struct stack_elem **stack, T data)
{
  struct stack_elem *s = malloc(sizeof *s);
  if (s)
  {
    s->data = data;   // new element gets data
    s->next = *stack; // set new element to point to current stack head
    *stack = s;       // new element becomes new stack head
  }
  return s != NULL;
}

int pop(struct stack_elem **stack, T *data)
{
  int stackempty = (*stack == NULL);

  if (!stackempty)
  {
    struct stack_elem *s = *stack; // retrieve the current stack head
    *stack = (*stack)->next;       // set stack head to point to next element
    *data = s->data;               // get the data
    free(s);                       // deallocate the element
  }

  return r;
}

int main(void)
{
  struct stack_elem *mystack = NULL; // stack is initially empty
  T value;
  ...
  if (!push(&mystack, some_data()))
    // handle overflow
  ...
  if (!pop(&mystack, &value))
    // handle underflow
  ...
}  

```

Since `push` and `pop` need to be able to write new pointer values to `mystack`, we need to pass a pointer to it, hence the double indirection for `stack` in `push` and `pop`.