[OT] Re: Why Is Escaping Data Considered So Magical?

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Mon Jun 28 20:26:43 EDT 2010


In message <mailman.2231.1277700501.32709.python-list at python.org>, Kushal 
Kumaran wrote:

> On Sun, Jun 27, 2010 at 5:16 PM, Lawrence D'Oliveiro
> <ldo at geek-central.gen.new_zealand> wrote:
>
>>In message <mailman.2184.1277626565.32709.python-list at python.org>, Kushal
>> Kumaran wrote:
>>
>>> On Sun, Jun 27, 2010 at 9:47 AM, Lawrence D'Oliveiro
>>> <ldo at 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:

ldo at 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*/
ldo at theon:hack> ./test
2 + 3 = 5




More information about the Python-list mailing list