views:

166

answers:

6

I have the following code :

int *edges[500];
char arr[] = {'c','d'};
edges[0] = arr;
printf("%c - %c", edges[0][0],edges[0][1]);

What I want displayed is c - d but what is actually being displayed is c -
So as you can see above, the first element is being displayed but not the second one.

Why isn't the second element of the array not being displayed ?

+2  A: 

Well, the

t.c:6: warning: assignment from incompatible pointer type

message might have something to do with it.

In C, 'c' is a (small) integer. You store those as chars in arr. However, by accessing arr as edges[0] which is an int *, you are actually retrieving (on most platforms) arr[0], arr[1], arr[2] and arr[3] as an integer.

Sinan Ünür
+3  A: 

Type mismatch. edges is an array of 500 pointers to int and you are assigning a pointer to two characters to its first element.

dirkgently
+3  A: 

edges[0] is interpreted as int array with elements {(int)'cd' , 0} (actually, the second element can contain any junk).

printf with specifier %c takes first byte of edges[0][0] (i.e. 'c') and first byte of edges[0][1] (which happened to be 0).

change type of edges to char* edgeds[500].

elder_george
+6  A: 

Since an int and a char have different sizes, you should try char *edges[500]

Jacob
Excellent; that was the problem.
Andreas Grech
A: 

edges is an array of pointers to int. The expression edges[0] yields a pointer to int, not pointer to char. Treating it as a char array causes the wrong byte offset to be calculated, causing the value of an unexpected piece of memory to be displayed rather than the memory holding the char value 'd'.

Array offsets calculate the offset in bytes based on the size of the type pointed to:

foo_t * x;
ptrdiff_t difference = x[1] - x[0];

The value of difference will be sizeof(foo_t) bytes. sizeof(char) is defined to be 1, and sizeof(int) is typically 4, meaning that your code will dereference a value 4 bytes into the char array, not 1 byte into the char array.

Tim
+1  A: 

line 3

edges[0] = arr;

incompatible types in assignment: arr is a char * and edges[0] is a int *.

What happens is this:

int *edges[500];
char arr[] = {'c','d'};

arr[0] is 'c' and arr[1] is 'd'

edges[0] = arr;

Ignoring the type compatibility, edges[0] point to the int at the address where the 'c' and 'd' (and possibly two more unspecified characters) are.

printf("%c %c", edges[0][0],edges[0][1]);

edges[0][0] is the first integer in edges[0]: that's the mix of 'c', 'd' (and possible two more unspecified characters). That value is converted to unsigned char, yielding 'c' which gets printed.
edges[0][1] points to the integer right after the mix of 'c', 'd' (and possibly two more unspecified characters). That memory location has not been initialized and it may well be outside the range your process can access.

if you print an int instead of 2 chars

printf("%d\n", edges[0][0]);

you will see the mix of 'c', 'd' (and possibly two unspecified characters).

Best thing to do is get your types right.

pmg
+1 for the explanation
Andreas Grech