Link to home
Start Free TrialLog in
Avatar of _marko_
_marko_

asked on

Compiles in WinNT but not in Linux

Hi!

I haven't worked with C that much so I'm still unused to the grammar of it. Below is a small piece of code which behaves oddly. I have tried to compile it with Borland 4.52, Visual C++ 6.0 and gcc (linux).

Here is the code:

#include <stdio.h>
#include <string.h>


struct VHDLPACKAGE {
     int width;
     int bits;
     char name[20];
     char rom[1][1];
};

int make_package ( VHDLPACKAGE &p );
int print_package ( VHDLPACKAGE &p );


int main ( void )
{

     VHDLPACKAGE package = {0,0,"ROM",{""}};
     make_package ( package );
     print_package ( package );

     return 0;
}

int print_package ( VHDLPACKAGE &p )
{

     printf ( "\n\n\n\nwidth: \t%d", p.width );
     printf ( "\n\n\n\nbits: \t%d", p.bits );
     printf ( "\n\n\n\nname: \t%s" , p.name );

     return 0;
}

int make_package ( VHDLPACKAGE &p )
{

     p.width = 15;
     p.bits = 16;

     strcpy ( p.name , "MyROM" );

     return 0;
}


Visual C++ 6.0: Compile & execution OK.
Borland 4.52: Compile & execution OK.
gcc (Linux): Did not compile. The used command was "gcc -o 2darray 2darray.c" It gave the following errors:

2darray.c:12: parse error before `&'
2darray.c:13: parse error before `&'
2darray.c: In function `main':
2darray.c:19: `VHDLPACKAGE' undeclared (first use in this function)
2darray.c:19: (Each undeclared identifier is reported only once
2darray.c:19: for each function it appears in.)
2darray.c:19: parse error before `package'
2darray.c: At top level:
2darray.c:22: warning: parameter names (without types) in function declaration
2darray.c:22: warning: data definition has no type or storage class
2darray.c:23: warning: parameter names (without types) in function declaration
2darray.c:23: warning: data definition has no type or storage class
2darray.c:25: parse error before `return'
2darray.c:28: parse error before `&'
2darray.c: In function `print_package':
2darray.c:31: `p' undeclared (first use in this function)
2darray.c: At top level:
2darray.c:38: parse error before `&'
2darray.c: In function `make_package':
2darray.c:41: `p' undeclared (first use in this function)

I don't have the slightest idea why the program compiled nicely in WinNT but not in Linux. Please advice how to change this code so that it will compile with gcc in Linux too because I need this to work in both environments (maybe it's a compiler switch i need to use?).

I have another question after I've gotten this snippet to work but I'll post a new question for that.

Marko
ASKER CERTIFIED SOLUTION
Avatar of SEve
SEve

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 3rsrichard
3rsrichard

It sounds like your gcc doesn't like your style of function declarations, ie;
 int make_package ( VHDLPACKAGE &p );
 int print_package ( VHDLPACKAGE &p );

You could try removing them, and moving main to the bottom, at least
that might give you new error messages. (which can sometimes be helpful.)
SEve is right! This is C++ syntax. You could change it to C standard, as suggested, or you can rename the file from file.c to file.C or file.cc to tell gcc that the source is C++.

Furthermore, your declaration char rom[1][1]; will generate a matrix with 1x1 (=1) character. Is this really what you want to do?
Avatar of _marko_

ASKER

SEve:
Thank you very much!

The C-programming course we had at our Polytechnic taught us to use references in stead of pointers because they are more tolerant (a reference to a null value doesn't crash as easily). But I didn't realize that references were only for C++. The course was kind of mixed C and C++ so it was hard to keep up with the C-only and C++-only stuff.

I also had problems learning pointer logic so I didn't mind using references throughout the course ;)

The typedef was totally new for me. Thanks!

The code you supplied compiled right away.

obg:
No, the matrix does not have the right dimensions yet. I will post a question about it in half an hour or so.

Marko