Re: I need help

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

  1. janus

    janus Guest

    On Monday, May 16, 2011 3:25:35 PM UTC+1, Shao Miller wrote:
    > On 5/16/2011 07:05, janus wrote:
    > >> Shao Miller
    > >> /* Join non-empty strings with a specified, non-empty delimiter string */
    > >> int join_strings_with_delim(
    > >> struct string * output_str,
    > >> const struct string * const * input_strings,
    > >> const unsigned int input_string_count,
    > >> const struct string * delim,
    > >> int allocation
    > >> ) {
    > >> int i;
    > >> size_t output_size;
    > >> char * buf;
    > >>
    > >> /* Check output string */
    > >> ...

    > >
    > > I am confuse... Could explain the following;
    > >
    > > const struct string * const * input_strings, # Having two consts and pointer symbol staggered

    >
    > Absolutely, I'm happy to explain. :)
    >
    > 'input_strings' is a pointer ('*') to a 'const'-qualified pointer ('*')
    > to a 'const'-qualified 'struct string'.
    >
    > ( [ (const struct string) * const] * input_strings)
    >
    > What does this madness mean? It means:
    >
    > - 'input_strings' is not 'const'-qualified, so it can be changed. For
    > example, you could do:
    >
    > input_strings++;
    >
    > - 'input_strings' points to a pointer. _That_ pointer is
    > 'const'-qualified, so it mustn't be changed. For example, you could not do:
    >
    > input_strings[0] = ...
    > *input_strings = ...
    >
    > - 'input_strings' points to a pointer, and _that_ pointer points to a
    > 'struct string'. That 'struct string' is 'const'-qualified, so it
    > mustn't be changed. For example, you could not do:
    >
    > input_strings[0][0].len = ...
    > (*input_strings)[0].len = ...
    > (*input_strings)->len = ...
    > (*(*input_strings)).len = ...


    Real madness =(
    janus, May 16, 2011
    #1
    1. Advertising

  2. janus

    Shao Miller Guest

    On 5/16/2011 13:15, janus wrote:
    >
    > Real madness =(


    If you remove every single instance of 'const' from the program, it can
    still be copied, pasted, and compiled.

    You can change the delimiter string to something else, and it should
    still give the desired results.

    The point of 'const'-qualifying things in that example program is to
    "protect" against programmer errors. There's no _need_ to modify some
    of the items, so we _prevent_ them from being modified by qualifying
    them with 'const'.

    If we are going to work with a 'const'-qualified object by passing a
    pointer to that object as a function argument, then the corresponding
    function parameter should include the 'const' qualifier, to ensure that
    the function's programmer does not accidentally modify the pointed-to
    object.

    Sooner or later, you might benefit from wrapping your mind around the
    'const' qualification business because...

    Just as Keith Thompson mentioned else-thread, the best approach to
    string literals when they are being used as an initializer for a pointer
    (rather than an array) is to treat the pointed-to string as 'const'.
    This prevents you from accidentally modifying the pointed-to string(s).
    But in order for functions to work with those strings, the functions
    _also_ have to treat the strings a 'const' (read-only).

    This bit is only really a concern for portable code, as a _particular_
    implementation might actually allow you to modify a string literal.
    Shao Miller, May 16, 2011
    #2
    1. Advertising

  3. Shao Miller <> writes:
    [...]
    > Just as Keith Thompson mentioned else-thread, the best approach to
    > string literals when they are being used as an initializer for a pointer
    > (rather than an array) is to treat the pointed-to string as 'const'.
    > This prevents you from accidentally modifying the pointed-to string(s).
    > But in order for functions to work with those strings, the functions
    > _also_ have to treat the strings a 'const' (read-only).
    >
    > This bit is only really a concern for portable code, as a _particular_
    > implementation might actually allow you to modify a string literal.


    I can be just as much of a concern for non-portable code. For example,
    I might write code that depends on gcc extensions, but gcc puts string
    literals in read-only memory. (I think gcc has an option to make string
    literals writable, but you get the point.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 16, 2011
    #3
  4. Keith Thompson <> writes:
    [...]
    > I can be just as much of a concern for non-portable code.

    [...]

    s/I/It/

    (must proffreed before posting)


    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 16, 2011
    #4
  5. janus

    Seebs Guest

    On 2011-05-16, Keith Thompson <> wrote:
    > Keith Thompson <> writes:
    > [...]
    >> I can be just as much of a concern for non-portable code.

    > [...]


    > s/I/It/


    > (must proffreed before posting)


    Huh, and here I was thinking I finally understood:
    pseudo.c, line 377: error: Keith Thompson.

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, May 16, 2011
    #5
  6. Seebs <> writes:
    > On 2011-05-16, Keith Thompson <> wrote:
    >> Keith Thompson <> writes:
    >> [...]
    >>> I can be just as much of a concern for non-portable code.

    >> [...]

    >
    >> s/I/It/

    >
    >> (must proffreed before posting)

    >
    > Huh, and here I was thinking I finally understood:
    > pseudo.c, line 377: error: Keith Thompson.


    I'd *love* to see the code that triggered that!

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, May 16, 2011
    #6
  7. janus

    Guest

    Keith Thompson <> wrote:
    > Seebs <> writes:
    > >
    > > Huh, and here I was thinking I finally understood:
    > > pseudo.c, line 377: error: Keith Thompson.

    >
    > I'd *love* to see the code that triggered that!


    Unfortunately, the margin of this post is too small to contain it.
    :)
    --
    Larry Jones

    Geez, I gotta have a REASON for everything? -- Calvin
    , May 16, 2011
    #7
    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,455
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    754
    Larry Marburger
    Jun 21, 2004
  3. John
    Replies:
    1
    Views:
    384
    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:
    541
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  5. ElementX
    Replies:
    9
    Views:
    478
    RedGrittyBrick
    Oct 1, 2008
Loading...

Share This Page