Click here to Skip to main content
15,356,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
[Error] invalid conversion from 'int' to 'const char*' [-fpermissive]
The above error occurs when I try to run the following code:

C
#include<stdio.h>
int main()
{
	int a = 10;
	int * ptr;
	ptr=&a;
	printf(ptr);
	printf("\n");
	printf(* ptr);
	printf("\n");
	printf(*(*(ptr)));
}

Thanks,
regards,
yeskay.

What I have tried:

I just declare a pointer variable and assigns the address of another variable. I try to print it. But I get this error:
[Error] invalid conversion from 'int' to 'const char*' [-fpermissive]
Posted
Updated 9-Feb-22 10:55am
v3
Comments
CPallini 9-Feb-22 2:01am
   
C is not Python...

1 solution

You need to review how printf() is used: printf, fprintf, sprintf, snprintf, printf_s, fprintf_s, sprintf_s, snprintf_s - cppreference.com[^]
In general printf() looks like
C
int printf(const char *format_string,  arg1, arg2, ...);
Where format_string describes the other arguments. So for example a format string of "%d" will treat the next argument as a integer, regardless of what it actually is. It is up to you, as the programmer to get things right. Fortunately, these days compilers are "printf aware" and can usually spot a problem and issue warnings as needed.
In your case you have
C
int *ptr;
printf(*ptr);
The warning you are getting is telling you that you are trying to convert the value of *ptr, which is an int with value 10 to a pointer type (char *), so its as if you wrote
C
printf(10)
10 is not a character string, so the compiler is letting you know that something is not right.
   
Comments
CPallini 9-Feb-22 2:01am
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900