This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Difference between C and C++


Hello!

Let's have the following C source:

---------- blah.c ----------
#include <stdio.h>

const char *nm[] =
{
  [1] = "zdar",
  [0] = "ahoj",
};

struct s
{
  int a;
  float b;
} si = { .a = 5, .b = 1.0 };

int main(void)
{
  printf("%s! %s!\n", nm[0], nm[1]);
  return 0;
}
----------------------------

It compiles and runs fine (gcc 2.95.3):

$ gcc blah.c -o blahc
$ ./blahc
ahoj! zdar!

When I rename blah.c to blah.cpp, it does not compile any more:

$ mv blah.c blah.cpp
$ gcc blah.cpp -o blahcpp
blah.c:5: parse error before `='
blah.c:13: parse error before `.'

I guess, that the standards of C and C++ differ in the regard of
initializing the arrays and structs, right?

I would like to reach the functionality of C's

enum { PIECE_A, PIECE_B, PIECE_C };

const PieceType pieces[] =
{
  [PIECE_A] = somethingA,
  [PIECE_B] = somethingB,
  [PIECE_C] = somethingC,
};

in my C++ source. I see more ways of implementation of it:
- initialize the array without the "[...] =" part
  What if I change the order in the enum in the future?
- initialize the array using some extra code
  Clean. But why that effort?
- PieceType could contain the info PIECE_A, _B or _C
  O(n) instead of O(1) for searching.

Do you see some other possibility? The cleaner the better :-)

Thank you,
Kendy


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]