-6

I have a linked-list. I want to put the current ptr sBuffer

each link is :

typedef struct{
   SearchContext *context;
   void *history;
   char id[32];
}ContextmenuContext;

but instead I'm getting the address of the next link.

I think "%p" is the bug.

what does printing "%p" do?

snprintf_safe(sBuffer,"%p",((ContextmenuContext*)current)->history);

Tom Seidel
  • 9,525
  • 1
  • 26
  • 38
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • 1
    print address, `%p` is a format string to print address, btw typecase to `void*` to use `%p` – Grijesh Chauhan Oct 22 '13 at 13:30
  • You need to pass a `void*` for `%p` otherwise it's undefined behaviour. – Simple Oct 22 '13 at 13:43
  • @Simple I think (hope) a `void const*` would work as well. But he is passing a `void*`, so that shouldn't be problem. (And this question should be labeled C, since it has nothing to do with C++.) – James Kanze Oct 22 '13 at 13:55
  • @JamesKanze ah you're right. I just saw the cast and didn't look at the rest. – Simple Oct 22 '13 at 15:02

1 Answers1

4

%p is intended to display the pointer address.

see printf syntax.

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • Note that that page contains a number of errors with regards to the types you pass to `printf`, depending on the specifiers. – James Kanze Oct 22 '13 at 13:54
  • See [`printf()` syntax](http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html) according to POSIX. – This isn't my real name Oct 22 '13 at 16:43
  • @ElchononEdelson Posix has several extensions, and using the `printf` of Posix is likely to cause portability problems down the road, when you port to Windows. – James Kanze Oct 22 '13 at 16:53
  • @JamesKanze You'll have problems with `printf()` anyway, when you port to Windows. Doesn't windows discourage all sorts of normal C library functions in favor of proprietary versions? But note that POSIX is strongly aligned with the ISO C standard, and the POSIX documentation is very, _very_ clear about what comes from C and what is an extension. – This isn't my real name Oct 22 '13 at 16:56
  • @ElchononEdelson Not in favor of proprietary versions, but in favor of those defined in Annex K of the C standard. This is one case where it is Unix which tries to lock you into their extensions. (And the format specifiers for `printf` _do_ have significant extensions in Unix.) – James Kanze Oct 22 '13 at 17:32