473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem on mixing C and C++ with a const struct (GCC compiler)

Hey ppl,

I got a little problem :/

I got the following C code in a header file and I am not allowed to change it:
Expand|Select|Wrap|Line Numbers
  1. const structTemp tmp = {.x = 5};
The code just works fine for GCC.

Now I got a project made with the Eclipse IDE in C++. And I am linking to the header file. Then I get the following error:
"expected primary-expression before '.' token"

What can I do to handle this problem? :(
I just tried to make a extern "C" {} around the header, but it does not work :(

Anyone got some help for me? :(

Greets
Jul 30 '08 #1
9 6282
arnaudk
424 256MB
I don't believe you. Firstly, because you are omitting the keyword 'struct': in C++, you are allowed to omit the struct keyword, but then you compile it with a C++ compiler like g++ not a C compiler like gcc.

Secondly, because this is not standard C or C++ syntax. You initialize a struct thus:
Expand|Select|Wrap|Line Numbers
  1. struct structTemp 
  2. {
  3.   int x;
  4.   char c;
  5. }; // definition
  6.  
  7. structTemp tmp1 = {5,'A'};        // initialization (in C++)
  8. struct structTemp tmp2 = {5,'A'}; // initialization (in C/C++)
  9. struct structTemp tmp3;           // delcaration (C/C++)
  10. tmp3.x = 5;                       // initialization
  11. tmp3.c = 'A';
  12.  
Jul 30 '08 #2
The struct is defined by something like:
SORRY, I now found the correct definition, and its not only struct, it looks like this:

Expand|Select|Wrap|Line Numbers
  1. typedef union
  2. {
  3.     struct
  4.     {
  5.          int h;
  6.          int l;
  7.     } x1;
  8.     unsigned int x;
  9. } structTemp;
  10.  
Then I got this code
Expand|Select|Wrap|Line Numbers
  1. const structTemp tmp = {.x = 5};
It compiles great with
Expand|Select|Wrap|Line Numbers
  1. gcc main.c -o main
where main.c links the header file with all this code in it.

When I link the header into my Eclipse project C++ code, I got the error from my first post.
Jul 30 '08 #3
arnaudk
424 256MB
In that case the answer is simple: while some C compilers support this method of initializing unions, pre-C99 and C++ does not. You'll have to use assignment instead of initialization.
Jul 30 '08 #4
In that case the answer is simple: while some C compilers support this method of initializing unions, pre-C99 and C++ does not. You'll have to use assignment instead of initialization.
Damn :( Ok, thanks :(

And how to assign a const variable? :(
Jul 30 '08 #5
Banfa
9,065 Expert Mod 8TB
You can't assign to a const variable, only initialise it but you could initialise in either of these 2 ways to get the same effect as your original code that is usable everywhere.

Expand|Select|Wrap|Line Numbers
  1. const structTemp tmp = {5, 0};
  2.  
  3. const structTemp tmp = {5};
  4.  
Jul 30 '08 #6
You can't assign to a const variable, only initialise it but you could initialise in either of these 2 ways to get the same effect as your original code that is usable everywhere.

Expand|Select|Wrap|Line Numbers
  1. const structTemp tmp = {5, 0};
  2.  
  3. const structTemp tmp = {5};
  4.  
Yeah, thats a good idea. Ill do it this way.

But when the structTemp looks like this:
Expand|Select|Wrap|Line Numbers
  1. typedef union
  2. {    
  3.            struct    
  4.            {    
  5.                 int h;    
  6.                 int l;    
  7.            } x1;    
  8.            unsigned int x;    
  9. } structTemp;
  10.  
Wouldnt be
Expand|Select|Wrap|Line Numbers
  1. const structTemp tmp = {0, 5};
be the right way?

I know they are union, but how should the compiler know, that the 5 is meant for variable x?

Greets,

Edit:
And what how could I initialize x1.h and x1.l?

Edit2:
Ok, I tried it out by myself:
const structTemp tmp = {5}; is right for making x = 5.
And:
const structTemp tmp = {{10, 5}}; is right for making h = 10 and l = 5.

Thx anyway :)
Jul 31 '08 #7
Ok, one last question about mixing C and C++.

If I got a variable called "delete" in my C code, is there a way to make the C++ ignore it?

Its C, but the C++ Compiler gives me an error cos delete is a keyword in C++.
With extern "C" {}; I can't solve the problem :(
Jul 31 '08 #8
Banfa
9,065 Expert Mod 8TB
If I got a variable called "delete" in my C code, is there a way to make the C++ ignore it?
If it is C then compile it with a C compiler.

If it is C++ then you can't use the keyword delete as a variable name.

If you are using a C++ compiler to compile it then it isn't C although it might be code that is compatible with both C and C++ compilers, which is in fact a subset of both languages which still would include not using C++ keywords as variable names.
Jul 31 '08 #9
If it is C then compile it with a C compiler.

If it is C++ then you can't use the keyword delete as a variable name.

If you are using a C++ compiler to compile it then it isn't C although it might be code that is compatible with both C and C++ compilers, which is in fact a subset of both languages which still would include not using C++ keywords as variable names.
Ok. Then I have to rename it. Thanks!
Jul 31 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Geiger Ho | last post by:
Hi all, Consider: a.hh ==== struct A { mytype_t attr1, mytype_t attr2,
10
by: Ardhendu Nandan | last post by:
I am trying to compile Gcc compiler in Windows O/S.I have downloaded entire source Code of this compiler at tries to run it in DEVCPP but I am gating some linker error and some header files missing...
6
by: bill | last post by:
I recently realized that I have a structure that I'd like to put more protection on in the following sense: I'd like to modify struct foo{ int *x; } ; to be: struct foo { const int *x;
5
by: Lee | last post by:
Hi I am not sure whether i am going off topic in asking this. It will be great help for me if any one could pinpoint locations where i could download the free gcc compiler to work on windows xp....
5
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members...
6
by: bramdoornbos | last post by:
Hello, I am looking for a solution to interface with C++ classes implemented in a dll compiled by gcc. This dll will be however accessed by a visual c++ compiled host (not made by me). Both...
6
by: mahesh | last post by:
Hi all, I am using Dev C++ IDE for the programming but for the time efficiency, I tried to use supercomputer (Linux) to run my program, but some of the function such as pow(double,double) ,...
1
by: Herhor | last post by:
I downloaded latest stable MinGW 5.1 but I have have already discovered that included gcc 3.4.2 is pretty old 2004 version. It is really strange because current GNU GCC version is 4.2.0! Is that...
5
by: arnaudk | last post by:
How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right... ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.