DEV Community

Discussion on: Data structures in C: Stack

Collapse
 
pauljlucas profile image
Paul J. Lucas • Edited

Generally, the type of the stack element is void* or, somewhat better, uint64_t. Then you can put any type you like in the stack. If sizeof(T) <= sizeof(uint64_t), then just cast the element types; otherwise dynamically allocated the Ts and store the pointers in the elements.

Also, most implementations aren't of fixed size; instead, each element has a next pointer. Lastly, if you use a singly linked list for the implementation, if you do just a bit more work, make it support full list operations and you get stack operations for free since a stack is a subset of a list.

Collapse
 
josethz00 profile image
José Thomaz • Edited

Thanks for the feedback, I already used void* but never uint64_t I will research about it.

Collapse
 
pauljlucas profile image
Paul J. Lucas

The reason for using uint64_t is because it's guaranteed to work on all platforms, specifically 32-bit platforms where sizeof(void*) is 4 — uint64_t guarantees 8.