How can I remove format warning in c language? - c

#include <stdio.h>
int main(void) {
int *pVar, var = 10;
pVar = &var;
//*pVar = var;
printf("value = %d, address = Ox%X\n", var, &var);
// Format specifies type 'unsigned int' but the argument has type 'int *'
printf("pValue = %d, address = Ox%X\n", *pVar, pVar);
// Format specifies type 'unsigned int' but the argument has type 'int *'
*pVar = 20;
printf("value = %d, address = Ox%X\n", var, &var);
// Format specifies type 'unsigned int' but the argument has type 'int *'
printf("pValue = %d, address = Ox%X\n", *pVar, pVar);
// Format specifies type 'unsigned int' but the argument has type 'int *'
return 0;
}
I find some warning
Format specifies type unsigned int but the argument has type int *
Even though the program runs as I intend, I want to know why this happen.
What should I do to remove those errors without making some errors in result?

You can use %p format for pointers as in
printf("value = %d, address = %p\n", var, (void *) &var);

Related

To print out the address of any variable [duplicate]

When i run this code.
#include <stdio.h>
void moo(int a, int *b);
int main()
{
int x;
int *y;
x = 1;
y = &x;
printf("Address of x = %d, value of x = %d\n", &x, x);
printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
moo(9, y);
}
void moo(int a, int *b)
{
printf("Address of a = %d, value of a = %d\n", &a, a);
printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}
I keep getting this error in my compiler.
/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
Could you help me?
Thanks
blargman
You want to use %p to print a pointer. From the spec:
p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
And don't forget the cast, e.g.
printf("%p\n",(void*)&a);
When you intend to print the memory address of any variable or a pointer, using %d won't do the job and will cause some compilation errors, because you're trying to print out a number instead of an address, and even if it does work, you'd have an intent error, because a memory address is not a number. the value 0xbfc0d878 is surely not a number, but an address.
What you should use is %p. e.g.,
#include<stdio.h>
int main(void) {
int a;
a = 5;
printf("The memory address of a is: %p\n", (void*) &a);
return 0;
}
Good luck!
To print the address of a variable, you need to use the %p format. %d is for signed integers. For example:
#include<stdio.h>
void main(void)
{
int a;
printf("Address is %p:",&a);
}
Looks like you use %p: Print Pointers
I tried in online compiler
https://www.onlinegdb.com/online_c++_compiler
int main()
{
cout<<"Hello World";
int x = 10;
int *p = &x;
printf("\nAddress of x is %p\n", &x); // 0x7ffc7df0ea54
printf("Address of p is %p\n", p); // 0x7ffc7df0ea54
return 0;
}

My compiler returned "assignment to 'int *' from incompatible pointer type 'char *' [-Wincompatible-pointer-types]" when I ran my C program

#include <stdio.h>
int main()
{
char grade = 'A';
int *p = &grade;
printf("The address where the grade is stored: %p\n", p);
printf("Grade: %c\n", grade);
return 0;
}
I get this error each time I compile the code on VScode but never on code blocks.
warning: incompatible pointer types initializing 'int *' with an
expression of type 'char *' [-Wincompatible-pointer-types]
int *p = &grade;
^ ~~~~~~
1 warning generated.
 ./main
The address where the grade is stored: 0x7ffc3bb0ee2b
Grade: A"
The warning tells you exactly what it is. It says:
incompatible pointer types initializing 'int *' with an expression of type 'char *'
This means that p is of type int *, that &grade is of type char *, and that these two pointers are not compatible. The solution is to change the declaration of p to:
char *p = &grade;
One more thing. Usually you can safely do implicit conversions to and from any pointer to void *, but not when passed as argument to a variadic function. If you want to print the address, use this:
printf("%p", (void *)p);
But only cast when needed. Never do it just to get rid of warnings. Here is an answer I wrote about that: https://stackoverflow.com/a/62563330/6699433
As an alternative, you could use a void pointer directly:
void *p = &grade;
printf("%p", p);
But then you would need to cast if you want to dereference it. For example:
char c = *(char*)p;
That cast is not necessary if p is declared as char *.

Incorrect format specifier with gcc compiler

This is a simple program in C to illustrate how C allocates memory to variables. The idea is that there will be a new memory address shown each time I re-compile the program.
Here is the program:
#include<stdio.h>
main()
{
int a;
int b = 10;
float c = 5.8;
printf("the address is %u \n", &a);
printf("the address is %u \n", &b);
printf("the address is &u \n", &c);
getch();
}
When I run this gcc program_name.c, I get a series of errors:
program_name.c:3:1: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
main()
^
program_name.c:9:33: warning: format specifies type 'unsigned int' but the
argument has type 'int *' [-Wformat]
printf("the address is %u \n", &a);
~~ ^~
program_name.c:10:33: warning: format specifies type 'unsigned int' but the
argument has type 'int *' [-Wformat]
printf("the address is %u \n", &b);
~~ ^~
program_name.c:11:33: warning: format specifies type 'unsigned int' but the
argument has type 'float *' [-Wformat]
printf("the address is %u \n", &c);
~~ ^~
program_name.c:13:2: warning: implicit declaration of function 'getch' is
invalid in C99 [-Wimplicit-function-declaration]
getch();
^
5 warnings generated.
Undefined symbols for architecture x86_64:
I cannot understand why these errors are being thrown. Surely %u is an entirely appropriate format specifier. What am I missing? How can I reformat the code to stop these errors?
EDIT: Based on the responses, it appears the correct code should be
#include<stdio.h>
int main()
{
int a;
int b = 10;
float c = 5.8;
printf("the address is %p \n", &a);
printf("the address is %p \n", &b);
printf("the address is %p \n", &c);
return 0;
}
That does run without errors.

c pointers - warning format '%x' expects arguments of type 'unsigned int'

I'm currently reading Hacking the art of exploitation and there is an example on there that I cannot seem to get correct. Attempting to compile results in the error:
./addressof.c: In function ‘main’:
./addressof.c:8:4: warning: format ‘%x’ expects argument of type ‘unsigned int’,
but argument 2 has type ‘int *’ [-Wformat]
#include <stdio.h>
int main() {
int int_var = 5;
int *int_ptr;
int_ptr = &int_var; // Put the address of int_var into int_ptr.
printf("int_ptr = 0x%08x\n", int_ptr);
printf("&int_ptr = 0x%08x\n", &int_ptr);
printf("*int_ptr = 0x%08x\n\n", *int_ptr);
printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n",
&int_ptr, int_ptr, *int_ptr);
}
I understand where the error is, I'm just not sure how to fix this.
The format specifier for pointer is %p, not %x. (See here)

How to print variable addresses in C?

When i run this code.
#include <stdio.h>
void moo(int a, int *b);
int main()
{
int x;
int *y;
x = 1;
y = &x;
printf("Address of x = %d, value of x = %d\n", &x, x);
printf("Address of y = &d, value of y = %d, value of *y = %d\n", &y, y, *y);
moo(9, y);
}
void moo(int a, int *b)
{
printf("Address of a = %d, value of a = %d\n", &a, a);
printf("Address of b = %d, value of b = %d, value of *b = %d\n", &b, b, *b);
}
I keep getting this error in my compiler.
/Volumes/MY USB/C Programming/Practice/addresses.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:17: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c: In function ‘moo’:
/Volumes/MY USB/C Programming/Practice/addresses.c:23: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int **’
/Volumes/MY USB/C Programming/Practice/addresses.c:24: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
Could you help me?
Thanks
blargman
You want to use %p to print a pointer. From the spec:
p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.
And don't forget the cast, e.g.
printf("%p\n",(void*)&a);
When you intend to print the memory address of any variable or a pointer, using %d won't do the job and will cause some compilation errors, because you're trying to print out a number instead of an address, and even if it does work, you'd have an intent error, because a memory address is not a number. the value 0xbfc0d878 is surely not a number, but an address.
What you should use is %p. e.g.,
#include<stdio.h>
int main(void) {
int a;
a = 5;
printf("The memory address of a is: %p\n", (void*) &a);
return 0;
}
Good luck!
To print the address of a variable, you need to use the %p format. %d is for signed integers. For example:
#include<stdio.h>
void main(void)
{
int a;
printf("Address is %p:",&a);
}
Looks like you use %p: Print Pointers
I tried in online compiler
https://www.onlinegdb.com/online_c++_compiler
int main()
{
cout<<"Hello World";
int x = 10;
int *p = &x;
printf("\nAddress of x is %p\n", &x); // 0x7ffc7df0ea54
printf("Address of p is %p\n", p); // 0x7ffc7df0ea54
return 0;
}

Resources