I am trying to implement a stack in C using arrays. I want an array of integers, and each time I try to push an int I want to allocate some new memory. But my understanding of malloc() is that it returns a pointer to some memory it allocated somewhere, which would be fine if I had an array of int-pointers, but I don't. Here's where I allocate new memory, and I get a warning:
int stack[] = {1}; // stack is allocated like this
stack[lasti + 1] = malloc(sizeof(int)); // assignment makes integer from pointer without a cast
Is it possible to implement a stack using a dynamically allocated array of non-pointers? Or does malloc() lend itself to just using an array of pointers?
EDIT: I think I am trying to treat an array like a linked list? Seems when I want to allocate space for an array using malloc, it looks something like malloc(sizeof(int) * maxSize). This will make a big chunk of memory somewhere. What I can't do is ask malloc to give me another piece of memory right at the end of that chunk, but I can expand that chunk. If I were implementing a stack using a linked list, it wouldn't matter WHERE malloc put the new space. I think I'm mixing some things up in my head.
So next question- if implementing a stack using arrays, do I HAVE to specify a maximum size of the stack?
malloc()allows to allocate memory block.realloc...stackis declared. And dynamic memory allocation without pointers is somewhat contradictory.stackonly has one element.stack[lasti + 1]is invalid.malloc.