C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-06-2009, 07:43 PM   #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
hotwheelharry is offline   Reply With Quote
Old 11-06-2009, 08:20 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,260
You can't use Console * inside the structure that defines Console in the first place. You need to typedef it first.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 11-06-2009, 09:01 PM   #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.
hotwheelharry is offline   Reply With Quote
Old 11-06-2009, 09:23 PM   #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.
MisterIO is offline   Reply With Quote
Old 11-07-2009, 01:08 AM   #5
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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.
C_ntua is offline   Reply With Quote
Old 11-07-2009, 05:37 AM   #6
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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?
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 11-07-2009, 09:23 AM   #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).
MisterIO is offline   Reply With Quote
Old 11-07-2009, 10:18 AM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 03:59 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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