1
#include <stdio.h>

int main() {
   int i;

   char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
   int int_array[5] = {1, 2, 3, 4, 5};

   char *char_pointer;
   int *int_pointer;

   char_pointer = int_array;
   int_pointer = char_array;

   for(i=0; i < 5; i++) {
      printf("[integer pointer] points to %p, which contains the integr %d\n", int_pointer, *int_pointer);
      int_pointer = int_pointer + 1;
   }

   for(i=0; i < 5; i++) {
      printf("[char pointer] points to %p, which contains the char '%c'\n", char_pointer, *char_pointer);
      char_pointer = char_pointer + 1;
   }
} 

Following error is displayed:

pointer_types2.c: In function ‘main’:
pointer_types2.c:13:17: warning: assignment from incompatible pointer type [enabled by default]
    char_pointer = int_array;
                 ^
pointer_types2.c:14:16: warning: assignment from incompatible pointer type [enabled by default]
    int_pointer = char_array;
                ^
wimh
  • 15,072
  • 6
  • 47
  • 98
Yeezus
  • 397
  • 1
  • 3
  • 14

3 Answers3

2

You can not do this because the storage size for char and int are different by a factor of 4. Specifically, you could try:

char_pointer = (char *)&int_array;
int_pointer = (int *)&char_array;

or

char_pointer = (char *)int_array;
int_pointer = (int *)char_array;

Which will compile without warning, but the result is:

output:

$ ./bin/juxt
[integer pointer] points to 0x7fff0e89d7d0, which contains the integr 1684234849
[integer pointer] points to 0x7fff0e89d7d4, which contains the integr 101
[integer pointer] points to 0x7fff0e89d7d8, which contains the integr 243914712
[integer pointer] points to 0x7fff0e89d7dc, which contains the integr 32767
[integer pointer] points to 0x7fff0e89d7e0, which contains the integr 243914672
[char pointer] points to 0x7fff0e89d7b0, which contains the char ''
[char pointer] points to 0x7fff0e89d7b1, which contains the char ''
[char pointer] points to 0x7fff0e89d7b2, which contains the char ''
[char pointer] points to 0x7fff0e89d7b3, which contains the char ''
[char pointer] points to 0x7fff0e89d7b4, which contains the char ''

NOTE: the increment in the int pointers d7d0, d7d4,... and compare to the character pointer values d7b0, d7b1,... Swapping the pointers in this fashion ends up with them pointing in the wrong place in memory.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
2

they are different types, you can't do that

pointer can only points to the objects which has the same type as itself. because different type have differen lenth in memory. for example, here, int has lenth of 4, and char has lenth of 1. when we do some operation on the pointer, here, +1, pointer moves with different steps, it may cause some error when you wanna access the data stored there.

however in some cases, it is safe to use pointer point to different type, it's advanced skill, be careful

simon_xia
  • 2,394
  • 1
  • 20
  • 32
  • Actually it's [valid](http://en.wikipedia.org/wiki/Pointer_aliasing#Aliasing_and_re-ordering) to point to `int` variable with `char *` (and `void *` as well) with explicit cast. However the reverse is not (meaning to point `char` variable with `int *`), due to possible alignment differences. Dereferencing of such pointer in latter case is undefined behavior, as it violates [strict aliasing rule](http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). – Grzegorz Szpetkowski Nov 16 '14 at 11:36
  • yep, in a more straightforward way, we can discrible it like this: we can use a short type pointer point to a long type(including equal), but can't use a long type pointer point to a short type. @Grzegorz Szpetkowski – simon_xia Nov 16 '14 at 11:58
  • 1
    @simon_xia: No, that's incorrect. `char` is the only exception. – Oliver Charlesworth Nov 16 '14 at 12:14
  • i got it. cause we can treat a block of memory as a array or char @Oliver Charlesworth – simon_xia Nov 16 '14 at 12:22
0

You can fix these warnings by type casting:

   char_pointer = (char *)int_array;
   int_pointer = (int *)char_array;
Gopi
  • 19,784
  • 4
  • 24
  • 36