I wrote the following C99 code and wondered about the structure declaration. In it, I declare two function pointers that ultimately point to two push / pop methods in the main code. In function pointer declarations, I bypassed the arguments and the program compiles normally. It's right? I am sure I read that arguments should be provided. Is this the correct behavior of the C99?
#include <stdio.h>
#define INITIAL_STACK_SIZE 1000
typedef struct stack
{
int index;
void *stack[INITIAL_STACK_SIZE];
void* (*Pop)();
void (*Push)();
} stack;
stack CreateStack(void);
void PushStack(stack*, void *);
void *PopStack(stack*);
stack CreateStack(void)
{
stack s = {0, '\0'};
s.Pop = PopStack;
s.Push = PushStack;
return s;
}
void PushStack(stack *s, void *value)
{
if(s->index < INITIAL_STACK_SIZE)
{
s->stack[s->index++] = value;
}
else
{
fputs("ERROR: Stack Overflow!\n", stderr);
}
}
void *PopStack(stack *s)
{
if(s->index > 0)
{
return s->stack[--s->index];
}
else
{
fputs("ERROR: Stack Empty!\n", stderr);
return NULL;
}
}
int main(int argc, char *argv[])
{
stack s = CreateStack();
s.Push(&s, "Hello");
s.Push(&s, "World");
printf("%s\n", (char*)s.Pop(&s));
printf("%s\n", (char*)s.Pop(&s));
return 0;
}
I tried adding arguments to function pointers, but I got a compiler error Extraneous old-style parameter list., so I assume this is correct, but will like a different opinion.
EDIT: " ", "typedef name" "stack" , struct "", , , .
Pelles C.