Error: invalid conversion from 'int' to 'int*' [-fpermissive] while initialising pointer address

I’m trying to assign the value of 0x0010 to pointer ptr1 in the following code that demonstrates how pointers interact with addition/subtraction:

#include <stdio.h>
int main(void) {
  int * ptr1=0x0010;
  double * ptr2=0x0010;
  printf("%p %p\n", ptr1+1, ptr1+2);  // Increment by 4 then 8
  printf("%p %p\n", ptr2+1, ptr2+2);  // Increment by 8 then 16

  printf("%p %p\n", ptr1, ptr2);
  ptr1++; // Increment by 4
  ptr2++; // Increment by 8
  printf("%p %p\n", ptr1, ptr2);
  return 0;
}

I am getting errors while compiling in the lines initializing the addresses of the pointers ptr1 and ptr2 (lines 3 and 4), error: invalid conversion from 'int' to 'int*' [-fpermissive]. Why are these two lines raising the error, and how can I avoid it?

>Solution :

"Why?"
Because the compiler is trying to prevent you from assigning an arbitrary address to a pointer (that you may later use).

"and how can I avoid it?"
Casting is used when the human programmer believes themself to be smarter than the compiler.

Try this:

  int *ptr1 = (int*)0x0010;
  double *ptr2 = (double*)0x0010;

and let the consequences be on your head…


"code that demonstrates how pointers interact with addition/subtraction"
Commonly referred to as pointer arithmetic.

Leave a Reply