title: Can we use a non-pointer as a pointer in C
body: <p>I'm fairly new to C but I was wondering, can we use something like an unsigned int like a void pointer. I wrote this short example to illustrate what I want to do but I can't seem to get it to work so I was wondering if it was even possible:</p>
  
  <pre><code>int main () {
  
      int i;
  
      char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
      int int_array[5] = {1, 2, 3, 4, 5};
  
      unsigned int unsigned_int;
  
      unsigned_int = (unsigned int) int_array;
  
      for(i=0; i &lt; 5; i++) {
      printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
      unsigned_int = unsigned_int + sizeof(int);
  
      }
  
      unsigned_int = (unsigned int) char_array;
  
      for(i=0; i&lt;5 ; i++) {
              printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
              unsigned_int = unsigned_int + sizeof(char);
      }
  </code></pre>
  
  <p>}</p>
  
  <p>The above code is what I want to get working but this next example is what I want it to work as:</p>
  
  <pre><code>int main () {
  
  int i;
  
  int int_array[5] = {1, 2, 3, 4, 5};
  char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
  
  void *void_pointer;
  
  void_pointer = (void *) int_array;
  
  for(i=0; i &lt; 5; i++) {
  printf("[integer pointer] has an adress of %p, and contains %d\n", void_pointer, *((int *)void_pointer));
  void_pointer = (void *) ((int *)void_pointer + 1);
  }
  
  void_pointer = (void *)char_array;
  for(i=0; i&lt;5; i++) {
  printf("[char pointer] has an adress of %p and contans %c\n", void_pointer, *((char *)void_pointer));
  void_pointer = (void *) ((char *)void_pointer + 1);
  }
  
  }
  </code></pre>
  
  <p>I understand that what I'm trying to do is probably unconventional but I really want to know if I can do this. When I try to compile the first one I get an error that says this: </p>
  
  <pre><code>pointer_type5.c: In function ‘main’:
  pointer_type5.c:12:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
           unsigned_int = (unsigned int) int_array;
                      ^
  pointer_type5.c:15:92: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
           printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
                                                                                          ^
   pointer_type5.c:20:24: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
           unsigned_int = (unsigned int) char_array;
                      ^
  pointer_type5.c:23:103: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
                   printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
  </code></pre>
  
  <p>When I run the second i get this:</p>
  
  <pre><code>[integer pointer] has an adress of 0x7fffcba7a5f0, and contains 1
  [integer pointer] has an adress of 0x7fffcba7a5f4, and contains 2
  [integer pointer] has an adress of 0x7fffcba7a5f8, and contains 3
  [integer pointer] has an adress of 0x7fffcba7a5fc, and contains 4
  [integer pointer] has an adress of 0x7fffcba7a600, and contains 5
  [char pointer] has an adress of 0x7fffcba7a5e0 and contans a
  [char pointer] has an adress of 0x7fffcba7a5e1 and contans b
  [char pointer] has an adress of 0x7fffcba7a5e2 and contans c
  [char pointer] has an adress of 0x7fffcba7a5e3 and contans d
  [char pointer] has an adress of 0x7fffcba7a5e4 and contans e
  </code></pre>
  
  <p>After taking your responses into consideration, I changed the original code to this:</p>
  
  <pre><code>int main () {
  
      int i;
  
      char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
      int int_array[5] = {1, 2, 3, 4, 5};
      unsigned int *unsigned_int;
  
      unsigned_int = (unsigned int *) int_array;
  
      for(i=0; i&lt;5; i++) {
              printf("[unsigned_int] points to %p, which contains the int %d\n", unsigned_int, *((int *) unsigned_int));
              unsigned_int = (unsigned int *) ((int *) unsigned_int + 1);
      }
  
      unsigned_int = (unsigned int *) char_array;
  
      for(i=0; i&lt;5 ; i++) {
              printf("[unsigned_int] points to %p, which contains the char '%c'\n", unsigned_int, *((char *) unsigned_int));
             unsigned_int = (unsigned int *)((char *) unsigned_int + sizeof(char));
      }
  </code></pre>
  
  <p>}</p>
  
  <p>Its not exactly what I was trying to do but I guess what I wanted to do can't be done so this code serves the same output. Editing the code led me to this next small question.
   Why can we use:</p>
  
  <pre><code>  unsigned_int = (unsigned int *)((char *) unsigned_int + sizeof(char));
  </code></pre>
  
  <p>and when we try it with int, it jumps too many bytes at a time and gives us random numbers. To fix that issue I just changed it to:</p>
  
  <pre><code> unsigned_int = (unsigned int *) ((int *) unsigned_int + 1);
  </code></pre>
  
  <p>But why didn't it work with sizeof(int)?</p>