Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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
0
_marko_
Asked:
_marko_
1 Solution
 
SEveCommented:
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
 
3rsrichardCommented:
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
 
obgCommented:
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
 
_marko_Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now