On Tue, Jun 29, 2010 at 5:56 AM, Lawrence D'Oliveiro <l...@geek-central.gen.new_zealand> wrote: > In message <mailman.2231.1277700501.32709.python-l...@python.org>, Kushal > Kumaran wrote: > >> On Sun, Jun 27, 2010 at 5:16 PM, Lawrence D'Oliveiro >> <l...@geek-central.gen.new_zealand> wrote: >> >>>In message <mailman.2184.1277626565.32709.python-l...@python.org>, Kushal >>> Kumaran wrote: >>> >>>> On Sun, Jun 27, 2010 at 9:47 AM, Lawrence D'Oliveiro >>>> <l...@geek-central.gen.new_zealand> wrote: >>>> >>>>> A long while ago I came up with this macro: >>>>> >>>>> #define Descr(v) &v, sizeof v >>>>> >>>>> making the correct version of the above become >>>>> >>>>> snprintf(Descr(buf), foo); >>>> >>>> Not quite right. If buf is a char array, as suggested by the use of >>>> sizeof, then you're not passing a char* to snprintf. >>> >>> What am I passing, then? >> >> Here's what gcc tells me (I declared buf as char buf[512]): >> sprintf.c:8: warning: passing argument 1 of ‘snprintf’ from >> incompatible pointer type >> /usr/include/stdio.h:363: note: expected ‘char * __restrict__’ but >> argument is of type ‘char (*)[512]’ >> >> You just need to lose the & from the macro. > > Why does this work, then: > > l...@theon:hack> cat test.c > #include <stdio.h> > > int main(int argc, char ** argv) > { > char buf[512]; > const int a = 2, b = 3; > snprintf(&buf, sizeof buf, "%d + %d = %d\n", a, b, a + b); > fprintf(stdout, buf); > return > 0; > } /*main*/ > l...@theon:hack> ./test > 2 + 3 = 5 >
By accident. I hope your compiler warned you about your snprintf call. Reading these threads might help you understand how char* and char (*)[512] are different: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/24708a9204061ce/848ceaf5ec774d81 http://groups.google.com/group/comp.lang.c.moderated/browse_thread/thread/fe264c550947a2e5/32b330cdf8aba3d6 -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list