LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with compiling C program, syntax error (https://www.linuxquestions.org/questions/programming-9/help-with-compiling-c-program-syntax-error-685809/)

MrUmunhum 11-24-2008 03:09 PM

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?

johnsfine 11-24-2008 03:37 PM

Quote:

Originally Posted by MrUmunhum (Post 3353132)
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.

raconteur 11-24-2008 06:18 PM

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[] = {
[...]


johnsfine 11-25-2008 07:31 AM

Quote:

Originally Posted by raconteur (Post 3353305)
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.

MrUmunhum 11-25-2008 02:05 PM

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"

:)

raconteur 11-26-2008 12:52 AM

Quote:

Originally Posted by johnsfine (Post 3353753)
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.

estabroo 11-26-2008 10:46 AM

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;


raconteur 11-26-2008 02:07 PM

Quote:

Originally Posted by estabroo (Post 3355476)
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).


All times are GMT -5. The time now is 12:13 AM.