LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-24-2008, 04:09 PM   #1
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA
Distribution: Debian/ Fedora
Posts: 435

Rep: Reputation: 33
Help with compiling C program, syntax error


Hi group,

I have a small problem with compiling a program. Here is the failing code:
Code:
/* c_lflag bits */
typedef  struct  LFlag {
  char*  Name;
  int    Flag; }
  
struct  LFlag  C_LFLAG[] = {  << Line 43
  
  { "isig",     0000001 },
  { "icanon",   0000002 },
  { "xcase",    0000004 },
  { "echo",     0000010 },
  { "echoe",    0000020 },
  { "echok",    0000040 },
  { "echonl",   0000100 },
  { "noflsh",   0000200 },
  { "tostop",   0000400 },
  { "echoctl",  0001000 },
  { "echoprt",  0002000 },
  { "echoke",   0004000 },
  { "flusho",   0010000 },
  { "pendin",   0040000 },
  { "iexten",   0100000 }  };
And this is the error:
Code:
gcc -g -I. showtty.c -o showtty
showtty.c:43: error: two or more data types in declaration specifiers
showtty.c:43: error: typedef ‘C_LFLAG’ is initialized (use __typeof__ instead)
Ideas? Suggestions?
 
Old 11-24-2008, 04:37 PM   #2
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by MrUmunhum View Post
Code:
/* c_lflag bits */
typedef  struct  LFlag {
  char*  Name;
  int    Flag; }
What do you think you're declaring there?

I expect what you intend is what you would have if you remove the "typdef" and add a ";" after "}"

Code:
/* c_lflag bits */
struct  LFlag {
  char*  Name;
  int    Flag; };
Otherwise, what you have is a typedef defining whatever name appears after the "}". But the next token after the "}" was the "struct" on line 43.
 
Old 11-24-2008, 07:18 PM   #3
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Just continuing the line of thought that johnsfine presented...
If you want to retain the type definition, then:
Code:
/* c_lflag bits */
typedef  struct  LFlag {
  char*  Name;
  int    Flag; };

LFlag  C_LFLAG[] = {
[...]
 
Old 11-25-2008, 08:31 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by raconteur View Post
Just continuing the line of thought that johnsfine presented...
If you want to retain the type definition, then:
Code:
/* c_lflag bits */
typedef  struct  LFlag {
  char*  Name;
  int    Flag; };
Is that valid C? I know many compilers will accept it, but are they correct to do so?

When I was fixing some code to make it compile with a newer version of GCC, I needed to remove "typedef" from several constructs that I'm pretty sure were equivalent to what you have above.
 
Old 11-25-2008, 03:05 PM   #5
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA
Distribution: Debian/ Fedora
Posts: 435

Original Poster
Rep: Reputation: 33
Smile Thanks

Thanks guys, here is what I got to work:
Code:
makeflag c_lflag.c;a.out
c_lflag.c
#include <stdio.h>
/* c_lflag bits */

struct  LFlag {
  char*  Name;
  int    Flag;  };
  
struct  LFlag  C_LFLAG[] = {
  
  {  "isig",     0000001  },
  {  "icanon",   0000002  },
  {  "xcase",    0000004  },
  {  "echo",     0000010  },
  {  "echoe",    0000020  },
  {  "echok",    0000040  },
  {  "echonl",   0000100  },
  {  "noflsh",   0000200  },
  {  "tostop",   0000400  },
  {  "echoctl",  0001000  },
  {  "echoprt",  0002000  },
  {  "echoke",   0004000  },
  {  "flusho",   0010000  },
  {  "pendin",   0040000  },
  {  "iexten",   0100000  },
  {  "",         0        } };

main( ) {
  int I;
  for( I = 0; ; I++ ) {
    if( C_LFLAG[I].Flag == 0 ) break;
    printf( "%5d \"%s\"\n", C_LFLAG[I].Flag, C_LFLAG[I].Name );
}
  return 0; }
/* ----------------------------------------------------------- */
    1 "isig"
    2 "icanon"
    4 "xcase"
    8 "echo"
   16 "echoe"
   32 "echok"
   64 "echonl"
  128 "noflsh"
  256 "tostop"
  512 "echoctl"
 1024 "echoprt"
 2048 "echoke"
 4096 "flusho"
16384 "pendin"
32768 "iexten"
 
Old 11-26-2008, 01:52 AM   #6
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by johnsfine View Post
Is that valid C? I know many compilers will accept it, but are they correct to do so?

When I was fixing some code to make it compile with a newer version of GCC, I needed to remove "typedef" from several constructs that I'm pretty sure were equivalent to what you have above.
Sure it is valid, straight out of the K&R bible. I prefer defining types to declaring structures in most instances, I find it most convenient.
Once one has the type defined, declaring instances and initializing them becomes quite simple. I also prefer to place type definitions in header files but that is a style preference.
 
Old 11-26-2008, 11:46 AM   #7
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,095
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
K&R isn't ansi though, typically if you want to typedef a struct on the same line you either have an anonymous struct or another name for the struct

Code:
typedef  struct  _LFlag_ {
  char*  Name;
  int    Flag;
} LFlag;
 
Old 11-26-2008, 03:07 PM   #8
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by estabroo View Post
K&R isn't ansi though, typically if you want to typedef a struct on the same line you either have an anonymous struct or another name for the struct
Agreed. I normally use both the type name and variable name when defining types just as a matter of course because I like to see the type name in symbolic debuggers and use the variable name when declaring instances. Again, that's pretty much a style preference except when I need the type to refer to itself within the type definition (the linked list thing).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error while compiling C++ Program ejoftiduttu Programming 1 01-23-2008 09:43 PM
Error in compiling Postgres FE C program skie_knite007 Programming 2 03-16-2007 11:38 PM
error compiling srgp program in c csst0136 Suse/Novell 0 10-23-2005 03:46 PM
error while compiling c /c++ program kiranbud Programming 1 10-12-2005 09:19 PM
error compiling a program minm Linux - Newbie 7 08-01-2005 11:40 AM


All times are GMT -5. The time now is 07:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration