Function pointer as a member of a C struct

后端 未结 5 1205
南笙
南笙 2020-12-02 07:08

I have a struct as follows, with a pointer to a function called \"length\" that will return the length of the chars member.

typedef struct pstring_t {
    ch         


        
5条回答
  •  独厮守ぢ
    2020-12-02 07:22

    My guess is that part of your problem is the parameter lists not matching.

    int (* length)();
    

    and

    int length(PString * self)
    

    are not the same. It should be int (* length)(PString *);.

    ...woah, it's Jon!

    Edit: and, as mentioned below, your struct pointer is never set to point to anything. The way you're doing it would only work if you were declaring a plain struct, not a pointer.

    str = (PString *)malloc(sizeof(PString));
    

提交回复
热议问题