views:

468

answers:

3

I'm reading about Type Equivalence in my Programming Languages class and I've come across a situation in C I'm unsure about.

It describes C's "Type Equivalence" as:

C uses a form of type equivalence that falls between name and structural equivalence, and which can be loosely described as "name equivalence for structs and unions, structural equivalence for everything else."

So what if I have two arrays of different size, but the same base type:

typedef int A1[10];  
typedef int A2[20];

Since all I need is structural equivalence, could these two be considered structurally equivalent? In C, is the size of the index set part of an array type or no?

+3  A: 

No they are not.

You can try sizeof(A1) and sizeof(A2) and see that they are different.

Mehrdad Afshari
+2  A: 

No they are not the same type.

In C you won't get an error, but in C++ if you try you will get an error something like:

error C2440: 'initializing' : cannot convert from 'int ()[1024]' to 'int ()[512]'

Here is some sample code

int x[1024];
int y[512];
/*Create a pointer int[1024] type*/
int (*px)[1024] = &x;
/*Create a pointer int[512] type*/
int (*py)[512] = &y;

int (*py2)[512] = &x; /*<---compiling error in C++ but allowed in C even know it's wrong*/

Even know that last line is allowed in C, the 2 types are considered distinct. Also an array is not the same as a pointer to an array, but as you can see the type declarations are different.

Brian R. Bondy
But in my book it says that Pointers are structurally equivelant in C. So, if an array is a pointer to n elements, would they not be structurally equivelant then?
Mithrax
An array is not a pointer - it can "decompose" to a pointer in some situations, but it is not equivalent to a pointer
1800 INFORMATION
you have too many medals :-)
Blank Xavier
A: 

In the source code, in some contexts, variables of different types are compatible in a given expression and the compiler will invoke sufficient magic to do the right/expected thing, for instance passing an array as an argument to a function expecting a pointer. However that does not mean that the memory layout of those are compatible. See question Pointer vs array in C, non-trivial difference where I missed on that.

I an not quite sure what your book means by "structural equivelance", but wonder if that maybe refers to the default integer promotion (but seems to also include arrays?). But in any case integer promotion is a very important issue and you should invest time in understanding. The printf below shall be executed in accordance to those rules:

unsigned int i = 0;
if (i < -1) {
        printf("This line is printed!\n");
}

The C standard (ISO/IEC 9899:1990) contains a section with title "usual arithmetic conversions" defining the behaviour. The standard is not freely available (on the contrary it is sold rather expensively since ISO standards are priced per page...), but if you search for that term you should be able to find some useful information or quotes. Some of the drafts of the standards are freely available, however threat those with a large amount of distrust since you do not know exactly what were changed to the final standard.

hlovdal
Your "Pointer vs array in C, non-trivial difference" link is broken, for me.
GMan
Really? I experience no problems with it, but in any case you should be able to search for the title.
hlovdal