[LC++]typedefs in C++

Palaka, Sasidhar psasi at corp.untd.com
Fri Sep 13 01:56:06 UTC 2002


Hi,
   I was reading 'Effective C++' by Scott Mayers and while 
going through the fifth Item, I came across this statement.

typedef string AddressLines[4];

Now, to be frank, I have never used such I construct in my 
coding. So, naturally I wanted to improve my understanding.
The following are the two sample programs I wrote.

1) 

#include <stdio.h>
typedef int nums[4];
main() 
{
  nums np;
  np[2] = 3;
  printf("My value = %d\n",np[2]);
}

Output I got: Compiles and runs both with gcc and g++


2) #include<stdio.h>
   typedef int nums[4];
   main()
   {
     nums np;
     np[2] = 3;

     int nq[4];
     nq[2] = 3;
     printf("My value = %d %d", np[2], nq[2]);
   }

Output I got: Compilation errors with gcc,
              Compiles and runs with g++.


Now, my question is why is not running with gcc in the 
second case? I guess, I am missing some funda with 
respect to the differences between C and C++ as far as 
'typedef's are concerned. Can somebody point out, what 
am I missing here?


Thanks,
Sasi.  






More information about the tuxCPProgramming mailing list