Thread: Simple struct definition error

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Simple struct definition error

    Hello all, I am new to C and have a small problem.
    I have defined the Console struct as follows...
    Code:
    typedef struct
    { 
      //console properties
      int Number;
      void (*Hide)(Console *);
      void (*Show)(Console *);
      short x;
      short y;
      short width;
      short height;
    } Console;
    but I get the following error...

    "Line 5: Expected ')' before '*' token"

    That is on the Hide function pointer line next to the last * star thing (@ char 23).
    I don't understand why, I thought that's how to define a function pointer in a struct that takes a pointer as an argument.

    If it makes any difference, this is going to be a program for a TI89 calculator.

    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't use Console * inside the structure that defines Console in the first place. You need to typedef it first.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2
    like...
    Code:
    typedef Console* ConsoleRef;
    
    typedef struct
    { 
      //console properties
      int Number;
      void (*Hide)(ConsoleRef);
      void (*Show)(ConsoleRef);
      short x;
      short y;
      short width;
      short height;
    } Console;
    How exactly did you mean?
    Last edited by hotwheelharry; 11-06-2009 at 09:04 PM.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by hotwheelharry View Post
    like...
    Code:
    typedef Console* ConsoleRef;
    
    typedef struct
    { 
      //console properties
      int Number;
      void (*Hide)(ConsoleRef);
      void (*Show)(ConsoleRef);
      short x;
      short y;
      short width;
      short height;
    } Console;
    How exactly did you mean?
    You're doing a typedef of struct "nothing" to console.
    And the first typedef doesn't work.
    An example that works:
    Code:
    #include <stdio.h>
    
    struct console;
    typedef struct console *console_ptr;
    
    typedef struct console {
    	int a;
    	console_ptr c_p;
    } console;
    
    int main(void)
    {
    	console c = {.a = 0, .c_p = NULL,};
    	printf("c.a = %d\n", c.a);
    	return 0;
    }
    Also those are not references, which don't even exist in c, but pointers.
    Last edited by MisterIO; 11-06-2009 at 09:50 PM.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    typedef struct myConsole
    { 
      ...
      struct myConsole* cpointer;
      ...
    } Console;
    Or do this. You have normally a struct myConsole and you name that struct "Console". Now you can use it as Console instead of "struct myConsole".

    In case you are wondering, you cannot replace above "struct myConsole*" with "Console*" inside the struct, cause it has not yet beend typedef.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please don't typedef pointers like this. They obfuscate the code!
    Is it really that difficult to type out the * to make it a pointer type?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    75
    Quote Originally Posted by Elysia View Post
    Please don't typedef pointers like this. They obfuscate the code!
    Is it really that difficult to type out the * to make it a pointer type?
    IMO there's no point in doing the typedef of struct console either, unless he wants to create an opaque type(which wouldn't make it necessary, but sometimes it's used).

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MisterIO View Post
    IMO there's no point in doing the typedef of struct console either, unless he wants to create an opaque type(which wouldn't make it necessary, but sometimes it's used).
    Gotta disagree with that if you mean that of these two:
    Code:
    typdef struct console;
    struct console;
    you prefer the later. But I do agree with Elysia about not including a * in typedef's.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM