Solved

Compiles in WinNT but not in Linux

Posted on 2001-06-27
4
276 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
Comment Utility
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
Comment Utility
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
Comment Utility
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_
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

771 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

14 Experts available now in Live!

Get 1:1 Help Now