0

Even though I‘m not a sheer expert on 'C' I wanted to give my nephew some beginner‘s lessons on pointer acrobatics. Therefore I needed to put an address into a string to let him read this. I studied two topics concerning ‘address to string‘:

In parts not flawlessly working

Not what I looked for

What I tried before,

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int Aa = 90;
// printf("\nAa got the address %p", &Aa); // of course working  !!
printf("\nAa got the address %x", &Aa);
printf("\nAa got the value %d \n", Aa);
return 0; }

resulted in a harmless but not at all welcomed warning:

format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘int*’ [-Wformat=]

and when I chose the simple way out of what I found:

...
char strAddress[] = "0x000000000"; 
sprintf(strAddress, "0x%x", &Aa);
...

of course I got the same warning with ‚sprintf‘ when trying to write to a DC.

The question is: How to avoid the warning when using just c-string?

longman
  • 9
  • 2
  • 4
    What warning do you get with %p? – user253751 Dec 28 '20 at 11:05
  • also did you consider uintptr_t? – user253751 Dec 28 '20 at 11:05
  • 2
    *Why doesn‘t printf/sprintf manage ‘%p‘ any longer*. It should. It would help if you tell us what exact error or incorrect behaviour you get when trying to use that. – kaylum Dec 28 '20 at 11:06
  • 2
    Re “Neither `printf("%p", &Aa);` nor `sprintf(strAddress, "%p", &Aa);` does the job”: Never describe a problem this way. It says nothing about what is wrong, so people cannot know what you expect or diagnose the problem. Say what happened when you tried this (quote the exact message from the compiler or the exact output produced by the program) and say what you expected instead. Edit the question to provide a [mre]. – Eric Postpischil Dec 28 '20 at 12:22
  • Re “Therefore I needed to put an address into a string to let him read this”: That “therefore” is dubious. There are ways to teach about pointers without converting them to strings. Is this string going to be read in a separate program execution from the one in which it is produced? – Eric Postpischil Dec 28 '20 at 12:23
  • I apologize for having asked the second question. I tried several ways on ubuntu-bionic, using just g++ or the autotools, writing to screen or to a DC and It always crashed at runtime. I got really used to it, that I thought the further development of ‘C‘ has bypassed me. Now on focal fossa I can‘t reproduce it. – longman Dec 30 '20 at 12:45
  • Thanks @user253751, your hint and google lead me directly here: [link](https://www.oreilly.com/library/view/understanding-and-using/9781449344535/ch01.html) – longman Dec 30 '20 at 13:11
  • Please do not significantly change your code after comments and/or answers were given that are rendered useless with your changed question. – Gerhardh Dec 30 '20 at 13:57
  • "a harmless but not at all welcomed warning" This warning is not harmless at all. It points out a severe error in your code. Pointers and integers are no guaranteed to have same size. – Gerhardh Dec 30 '20 at 14:01

3 Answers3

0

Do you like this?

#include <stdio.h>
#include <inttypes.h>

int main() {
    int Aa = 90;
    printf("0x%" PRIxPTR "\n", (uintptr_t)&Aa);
}

Outputs something like: 0x7ffddc89907c.

Anyway, "%p" must work lol.

0

If you want to output the address, just use %p and & to return the address of the variable.

#include<stdio.h>

int Aa = 90;

void main()
{
    printf("%p", (void *) &Aa);
}

And note that to use printf you have to include the stdio.h .

DariushStony
  • 153
  • 8
0

To print an address, use %p format specifier, and cast the argument to void*. Like

#include<stdio.h>

int main (void)
{
    int var = 12345;
    printf("The address is %p\n", (void*) &var); // add 0x in front of %p if you like

    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261