Re: I need help

Discussion in 'C Programming' started by janus, May 16, 2011.

  1. janus

    janus Guest


    > --
    > Ian Collins

    You must not use the macro STRNCAT in e.g. this case:

    void a_procedure(char *a)
    {
    STRNCAT(a, "Jersey");
    }

    int main(void)
    {

    char string[10] = "NEW";
    a_procedure(string);
    }

    Here sizeof(a) is the size of a pointer and not the size of string. Instead
    you could do it without the macro:

    void a_procedure(char *a, size_t size)
    {
    strncat(a, "Jersey", size - strlen(a) - 1);
    }

    int main(void)
    {

    char string[10] = "NEW";
    a_procedure(string, sizeof(string))

    On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
    > "Ian Collins" <> schrieb im Newsbeitrag
    > news:...
    > > On 05/15/11 06:29 AM, Heinrich Wolf wrote:
    > >>
    > >> or with a macro:
    > >>
    > >> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
    > >> char string[10] = "NEW";
    > >> STRNCAT(string, "Jersey");

    > >
    > > That macro would go horribly (and silently) wrong if
    > >
    > > a) 'a' wasn't a fixed sized array, or
    > > b) it was, but the definition wasn't visible when the macro was expanded.
    > >
    > > --
    > > Ian Collins

    >
    > You must not use the macro STRNCAT in e.g. this case:
    >
    > void a_procedure(char *a)
    > {
    > STRNCAT(a, "Jersey");
    > }
    >
    > int main(void)
    > {
    >
    > char string[10] = "NEW";
    > a_procedure(string);
    > }
    >
    > Here sizeof(a) is the size of a pointer and not the size of string. Instead
    > you could do it without the macro:
    >
    > void a_procedure(char *a, size_t size)
    > {
    > strncat(a, "Jersey", size - strlen(a) - 1);
    > }
    >
    > int main(void)
    > {
    >
    > char string[10] = "NEW";
    > a_procedure(string, sizeof(string));
    > }


    I do not see any difference between the two
    Janus
    janus, May 16, 2011
    #1
    1. Advertising

  2. "janus" <> schrieb im Newsbeitrag
    news:...
    ....
    > void a_procedure(char *a)

    char * is a pointer and not an array. On a 32 bit machine sizeof(a) is
    normally 4 (4 bytes, 8 bit each)
    ....
    >> char string[10] = "NEW";


    sizeof(string) is 10 here.

    ....

    > I do not see any difference between the two
    > Janus
    Heinrich Wolf, May 16, 2011
    #2
    1. Advertising

  3. janus

    James Kuyper Guest

    On 05/16/2011 06:52 AM, janus wrote:
    ....
    > On Monday, May 16, 2011 11:26:30 AM UTC+1, Heinrich Wolf wrote:
    >> "Ian Collins" <> schrieb im Newsbeitrag
    >> news:...
    >>> On 05/15/11 06:29 AM, Heinrich Wolf wrote:
    >>>>
    >>>> or with a macro:
    >>>>
    >>>> #define STRNCAT(a, b) strncat(a, b, sizeof(a) - strlen(a) - 1)
    >>>> char string[10] = "NEW";
    >>>> STRNCAT(string, "Jersey");
    >>>
    >>> That macro would go horribly (and silently) wrong if
    >>>
    >>> a) 'a' wasn't a fixed sized array, or
    >>> b) it was, but the definition wasn't visible when the macro was expanded.
    >>>
    >>> --
    >>> Ian Collins

    >>
    >> You must not use the macro STRNCAT in e.g. this case:
    >>
    >> void a_procedure(char *a)
    >> {
    >> STRNCAT(a, "Jersey");
    >> }
    >>
    >> int main(void)
    >> {
    >>
    >> char string[10] = "NEW";
    >> a_procedure(string);
    >> }
    >>
    >> Here sizeof(a) is the size of a pointer and not the size of string. Instead
    >> you could do it without the macro:
    >>
    >> void a_procedure(char *a, size_t size)
    >> {
    >> strncat(a, "Jersey", size - strlen(a) - 1);
    >> }
    >>
    >> int main(void)
    >> {
    >>
    >> char string[10] = "NEW";
    >> a_procedure(string, sizeof(string));
    >> }

    >
    > I do not see any difference between the two
    > Janus


    In the first version of a_procedure, STRNCAT(a, "Jersey") expands to

    strncat(a, "Jersey", sizeof(a) - strlen(a) - 1)

    In that expression, sizeof(a) gives the size of the variable 'a', which
    is a pointer to char. Therefore, sizeof(a) will be equal to
    sizeof(char*). There's a pretty good chance that this will be either 4
    or 8, depending upon what platform you're compiling for. It's unlikely
    to be 10, though that's not an impossible value.

    In the second version, sizeof(string) gives the size of the variable
    "string", which is an array 10 char elements, so it is guaranteed to be
    10. It's unlikely that this will be the same as sizeof(a); but even if
    it is, that's only a coincidence.
    --
    James Kuyper
    James Kuyper, May 16, 2011
    #3
  4. janus

    Ian Collins Guest

    On 05/16/11 10:52 PM, janus wrote:

    Please stop starting new threads! Drop that awful new google interface.

    --
    Ian Collins
    Ian Collins, May 16, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,471
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    765
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    394
    John C. Bollinger
    Aug 19, 2004
  4. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    560
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    491
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page