Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading