i need to pass an int or a string into a push function for a stack. normally i would just overload the function and have one that takes in a string parameter and one that takes in an int parameter so that the appropriate function would be called just based off of the parameters. i wrote the spots in comments where i would normally include the type. i just got stuck there.
void push(Stack *S, /* int or a string */ element)
{
/* If the stack is full, we cannot push an element into it as there is no space for it.*/
if(S->size == S->capacity)
{
printf("Stack is Full\n");
}
else
{
/* Push an element on the top of it and increase its size by one*/
if (/* element is an int*/)
S->elements[S->size++] = element;
else if (/* element is a string */)
S->charElements[S->size++] = element;
}
return;
}
There is no function overloading in c.
You could pass the type in as an argument, make the element parameter a pointer and then re-cast the pointer to the appropriate type.