C struct syntax query

This is a discussion on C struct syntax query within the C Programming forums, part of the General Programming Boards category; I've vastly simplified the struct definitions to minimise the size of the code, but the meaning is the same. The ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    C struct syntax query

    I've vastly simplified the struct definitions to minimise the size of the code, but the meaning is the same. The query is with the definition of SCIP_Reader.

    Once the macro define has been applied, the line in SCIP_Reader becomes :
    Code:
    SCIP_RETCODE (*readerfree) (SCIP* scip, SCIP_READER* reader);
    Although I understand the first half, I don't understand what the second half (the scip and reader declarations) means in this particular case, and when I try to compile, I get the error:
    test.c:18: error: expected declaration specifiers or ‘...’ before ‘SCIP_READER’
    The error makes sense, I guess, as as far as I know, you can only declare one variable per line, not three.

    I am not trying to get the code to work, as that misses the point. I merely want to understand the above particular line.

    Code:
    #define SCIP_DECL_READERFREE(x) SCIP_RETCODE x (SCIP* scip, SCIP_READER* reader)
    
    enum SCIP_Retcode
    {
    	SCIP_OKAY  = 1,
    	SCIP_ERROR = 0
    };
    typedef enum SCIP_Retcode SCIP_RETCODE;
    
    struct Scip
    {
    	int id;
    };
    typedef struct Scip SCIP;
    
    struct SCIP_Reader
    {
    	SCIP_DECL_READERFREE ((*readerfree));
    	// After applying the macro at line #1, the above line becomes
    	//SCIP_RETCODE (*readerfree) (SCIP* scip, SCIP_READER* reader);
    };
    typedef struct SCIP_Reader SCIP_READER;
    
    int main(void)
    {
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by thealmightyone View Post
    The error makes sense, I guess, as as far as I know, you can only declare one variable per line, not three.
    Wha? There is only one declaration on the line in question. Don't know what train of logic you're on there, but that's not the reason for the error.

    When the code is expanded to:

    Code:
    SCIP_RETCODE (*readerfree) (SCIP* scip, SCIP_READER* reader);
    The compiler hasn't yet seen anything that tells it what type SCIP_READER is. Try putting typedef that declares this before the struct definition (yes, you can do this) like so:

    Code:
    typedef struct SCIP_Reader SCIP_READER;
    struct SCIP_Reader
    {
    	SCIP_DECL_READERFREE ((*readerfree));
    	// After applying the macro at line #1, the above line becomes
    	//SCIP_RETCODE (*readerfree) (SCIP* scip, SCIP_READER* reader);
    };

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Quote Originally Posted by JohnGraham View Post
    The compiler hasn't yet seen anything that tells it what type SCIP_READER is. Try putting typedef that declares this before the struct definition (yes, you can do this) like so:
    That fixed the compilation error.

    If there aren't three declarations, but just one, then what does "(SCIP* scip, SCIP_READER* reader)" do, as it appears to contain two further variable declarations.

    I also have a query regarding "SCIP_RETCODE (*readerfree)" - is this equivalent to "SCIP_RETCODE* readerfree"?
    Last edited by thealmightyone; 11-22-2010 at 09:47 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by thealmightyone View Post
    If there aren't three declarations, but just one, then what does "(SCIP* scip, SCIP_READER* reader)" do, as it appears to contain two further variable declarations.
    The (one) declaration is of a variable named "readerfree", which is a function pointer. If you don't know what they are, then Google will give you some good tutorials. In this case "(SCIP* scip, SCIP_READER* reader)" are the function parameters.


    Quote Originally Posted by thealmightyone View Post
    I also have a query regarding "SCIP_RETCODE (*readerfree)" - is this equivalent to "SCIP_RETCODE* readerfree?
    No - as you'll see if you read-up on function pointers, "SCIP_RETCODE (*readerfree) (...)" declares "readerfree" to be a pointer to a function that returns a value of type SCIP_RETCODE, whereas "SCIP_RETCODE* readerfree (...)" declares readerfree to be a function that returns a value of type pointer-to-SCIP_RETCODE.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Quote Originally Posted by JohnGraham View Post
    The (one) declaration is of a variable named "readerfree", which is a function pointer. If you don't know what they are, then Google will give you some good tutorials. In this case "(SCIP* scip, SCIP_READER* reader)" are the function parameters.




    No - as you'll see if you read-up on function pointers, "SCIP_RETCODE (*readerfree) (...)" declares "readerfree" to be a pointer to a function that returns a value of type SCIP_RETCODE, whereas "SCIP_RETCODE* readerfree (...)" declares readerfree to be a function that returns a value of type pointer-to-SCIP_RETCODE.
    Thanks for the explanations John, it all make sense now. Really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21