¿Cómo imprimir direcciones variables en C?
Cuando corro este código.
#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);
}
Sigo recibiendo este error en mi compilador.
/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 *’
¿Podría ayudarme?
Gracias
Blargman
4 answers
Desea usar %p
para imprimir un puntero. De la especificación:
p
El argumento debe ser un puntero avoid
. El valor del puntero se convierte en una secuencia de caracteres de impresión, de una manera definida por la implementación.
Y no se olvide del molde, por ejemplo,
printf("%p\n",(void*)&a);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-31 13:28:28
Cuando intenta imprimir la dirección de memoria de cualquier variable o puntero, usar %d
no hará el trabajo y causará algunos errores de compilación, porque está tratando de imprimir un número en lugar de una dirección, e incluso si funciona, tendría un error de intent, porque una dirección de memoria no es un número. el valor 0xbfc0d878
seguramente no es un número, sino una dirección.
Lo que debes usar es %p
. por ejemplo,
#include<stdio.h>
int main(void) {
int a;
a = 5;
printf("The memory address of a is: %p\n", (void*) &a);
return 0;
}
¡Buena suerte!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-13 06:20:17
Parece que usa %p: Punteros de impresión
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-12 23:59:20
Para imprimir la dirección de una variable, debe usar el formato %p
. %d
es para enteros. Por ejemplo:
#include<stdio.h>
void main(void)
{
int a;
printf("Address is %p:",&a);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-20 06:44:58