I am writing some code to get a hex dump of the stack in c. I keep getting a compile error on this following line when I compile it using gcc in ubuntu however it compiles fine under gcc in windows.
char buffer[10];
for (int i=0;i<20;i++)
printf("0x%lx => 0x%lx\n", &(buffer[i]), ((long *)buffer)[i]);
This is the message the compiler gives.
warning: format ‘%lx’ expects type ‘long unsigned int’, but argument 2 has type ‘char *’
Can someone please tell me if I am doing someting wrong?
Try:
The 2nd arg,
&(buffer[i])is of typechar*, so it needs a cast and a%p.The 3rd arg,
((long *)buffer)[i], is of typelong, so it needs a%lx.Aside: Please realize that if
bufferis notlong-aligned, you might get the right answer, the wrong answer, or a core dump, all depending upon your CPU, OS, OS settings, and/or compiler.If it were me, I’d try: