Implementation of an Stack without Linked Lists in C I'm a beginning C programmer. As an exercise, I have attempted to implement a stack of strings in C. This is my first real exercise working with pointers. I looked at some other similar posts on here, and read through the comments. It seems that most individuals have chosen to do this by using linked lists, whereas I've just created an ArrayStack. 

Maybe it would have been better to go the linked list route. Nonetheless, this is how I did it. I'm hoping there's not any glaring errors, but I'm sure there will be.

Also, question: I have a variable for the number of slots available in the array, which is used to determine when more memory needs to be allocated. But it would seem to me that I can use sizeof on the array, and sizeof on a single pointer in the array, and use division to get the number of slots, thus eliminating a variable. Would this be a better approach?

stack.h

#include &lt;stdlib.h&gt;

typedef struct stringstack {
    int count;
    int slots;
    char** stack;
} stringstack;

void * makestack(void);
void push(struct stringstack* ptr, char stringtext[]);
char * pop(struct stringstack* ptr);
char * peek(struct stringstack* ptr);
int isempty(struct stringstack* ptr);
void destroy(struct stringstack* ptr);


stack.c

#include "stack.h"

/* Function creates a new stringstack object and returns a pointer to it */
void * makestack(void) {
    stringstack *ptr = (stringstack *) malloc(sizeof(stringstack));
    ptr-&gt;count = 0;
    ptr-&gt;slots = 1;
    ptr-&gt;stack = (char **) malloc(sizeof(char*));
    return ptr;
}

/* Pushes a string onto the top of a stack */
void push(struct stringstack* ptr, char stringtext[]) {
    ptr-&gt;count++;
    if (ptr-&gt;count == ptr-&gt;slots) /* Allocate more space if necessary */
        ptr-&gt;stack = (char **) realloc(ptr-&gt;stack, sizeof(char *)*((ptr-&gt;slots*=2)+1));
    char *stringptr = (char *) malloc(sizeof(stringtext));
    stringptr = stringtext;
    *(ptr-&gt;stack + ptr-&gt;count) = stringptr;
}

/* Removes the top item from the stack, returning a pointer to it */
char * pop(struct stringstack* ptr) {
    if (ptr-&gt;count&gt;0)
        return *(ptr-&gt;stack + ptr-&gt;count--);
    return NULL;
}

/* Returns a pointer to top item from stack but does not pop it */
char * peek(struct stringstack* ptr) {
    if (ptr-&gt;count&gt;0)
        return *(ptr-&gt;stack + ptr-&gt;count);
    return NULL;
}

/* Returns whether a stringstack contains at least one string */
int isempty(struct stringstack* ptr) {
    return (ptr-&gt;count == 0);
}

/* Frees a stringtack from memory */
void destroy(struct stringstack* ptr) {
    size_t n;
    for (n = 0; n &lt;= ptr-&gt;count; n++ )
        free(ptr-&gt;stack+n);
    free(ptr-&gt;stack);
    free(ptr);
}
makestack should return a struct stringstack * instead of void *.
malloc and realloc may fail. Check their return values.
push doesn't copy the string (it only makes a redundant copy of the pointer). It is OK as long as you can guarantee that the string parameter has a lifetime long enough. Consider a scenario:

void foo(struct stringstack * stack)
{
    char bar[] = "aaa";
    push(stack, bar);
}


Upon the return, bar disappears, and the stack entry points to the pretty random place in the application stack. Do not expect to have "aaa" there.

To actually copy the string, do

 *(ptr-&gt;stack + ptr-&gt;count) = strdup(stringtext);


which may introduce other problems in interacting with client (who owns the string after peek?). Unfortunately, C doesn't offer a clean solution.
You may want to shrink the stack when ptr-&gt;count hits the low watermark of ptr-&gt;slots.