say I have the following:
#include <stdio.h>
#include <string.h>
int main() {
char c = 'c';
char addr[50];
strcpy(addr, &c);
printf("%p\n", &c);
printf("%s", addr);
printf("\n");
return 0;
}
The above would print
0x7ffc241780af
c@@ //some gibberish
for the second line of the output, I am intending to output the char array so that it prints same as the first line. I am wishing to have memory address as a string so that I can manipulate with it, but how exactly do I get the memory address as a char string?
Try this instead:
sprintf(addr, "%p", &c);
printf("%s\n", addr);
The sprintf function is used to "print" to a string. So instead of
strcpy(addr, &c);
printf("%p\n", &c);
printf("%s", addr);
printf("\n");
try
sprintf( addr, "%p", (void *)&c );
printf( "%p\n", (void *)&c );
printf( "%s\n", addr );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With