开发者

Is it correct C99 that you don't need to specify arguments in function pointer declarations in structs?

I have written the following C99 code and was wondering about the struct declaration. In it i declare two function pointers which ultimately point to the two push/pop methods in the main code. In the function pointer declarations i've ommited the arguments and the program compiles ok. Is this correct? I'm sure i've read that the arguments must be supplied. Is this correct C99 behaviour?

#include <stdio.h>

#define INITIAL_STACK_SIZE 1000

typedef struct stack
{
    int index;
    void *stack[INITIAL_STACK_SIZE];
    void* (*Pop)(); //<-- Is this correct?
    void (*Push)(); //<-- Is this correct?
} 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 the arg开发者_JAVA技巧uments to the function pointers but i got a compiler error of Extraneous old-style parameter list. so i'm guessing it's correct, but would love another opinion.

EDIT: I was experiencing the above 'Extraneous old-style parameter list' error because i was using the typedef name 'stack' rather than using the struct keyword with 'stack' to define it was the structure i am currently defining.

I'm using the Pelles C compiler.


That is bad style (although legal). It will work but it means the compiler can't check your arguments. So if you accidentally call your function like this:

s.Push(arg, &s);   // oops, reverse the arguments

the compiler won't be able to tell you the call is bad.

Prior to ANSI standardization, K&R C did not have prototypes; it only supported declarations that specified the return type. When you leave out the arguments, you are using this archaic feature.

On gcc, you can use the option -Wstrict-prototypes to enable warnings when you use them.


Though it also does under GCC with std=c99 -Wall -pedantic without even a warning about it, I'm surprised it compiles at all. In my opinion, that's not pretty cool.

I believe it's a much better idea to use the following:

void* (*Pop)(struct stack*);
void (*Push)(struct stack*, void*);

It does compile under GCC 4.2 using the above switches.

Otherwise, looking at your code, I could very well think you're doing a mistake by calling Push with two arguments. The above also compiles and clears that confusion.


gcc (with -pedantic -Wall -std=c99) has no problem with this code:

typedef struct stack
{
 int index;
 void *stack[INITIAL_STACK_SIZE];
 void* (*Pop)(struct stack *);
 void (*Push)(struct stack *, void *);
} stack;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜