RE: [C] Type Ambiguous Stack 01-03-2015, 05:48 PM
#5
(01-03-2015, 05:37 PM)0xDEAD10CC Wrote: The other thing you could do if you wanted actual stack types, is define whichever ones you want, using macros:
Code:/* * Author: 0xDEAD10CC * Date: 2015-01-01 16:04:36 * Filename: main.c * Last Modified time: 2015-01-03 09:37:35 */ #include <stdio.h> #include <stdlib.h> #define def_stack_t(T) \ struct stack_t_##T \ { \ T *p_base; \ T *data; \ }; #define stack_t(T) struct stack_t_##T def_stack_t(int) def_stack_t(float) int main(void) { stack_t(int) *stack_obj = ...; // deal with stack object here free(stack_obj); }
Now you can define generic stack functions that work with void *, or you can create an army of macros to do all of the calculations for the sizes for you too.
That is a way to do a "template" stack in C, but not a real stack. Since you are only able to put integers on that stack.



























![[+]](https://sinister.li/images/modern/collapse_collapsed.png)