Solved

Compiles in WinNT but not in Linux

Posted on 2001-06-27
4
277 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:_marko_
4 Comments
 
LVL 3

Accepted Solution

by:
SEve earned 50 total points
ID: 6231115
hi, this is correct code for unix/linux:

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

/* Have to use typedef to define VHDLPACKAGE */
typedef struct VHDLPACKAGE {
    int width;
    int bits;
    char name[20];
    char rom[1][1];
} VHDLPACKAGE;

/* Here you used references which are C++ feature */
/* You should use pointers instead of references  */
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;
}


regards, seve
0
 
LVL 3

Expert Comment

by:3rsrichard
ID: 6231126
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.)
0
 
LVL 2

Expert Comment

by:obg
ID: 6231212
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?
0
 
LVL 2

Author Comment

by:_marko_
ID: 6231234
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
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now