[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: I need help

janus

5/16/2011 5:15:00 PM

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 =(
6 Answers

Shao Miller

5/16/2011 6:04:00 PM

0

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.

Keith Thompson

5/16/2011 6:37:00 PM

0

Shao Miller <sha0.miller@gmail.com> 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) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson

5/16/2011 7:23:00 PM

0

Keith Thompson <kst-u@mib.org> 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) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Seebs

5/16/2011 7:33:00 PM

0

On 2011-05-16, Keith Thompson <kst-u@mib.org> wrote:
> Keith Thompson <kst-u@mib.org> 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 / usenet-nospam@seebs.net
http://www.seeb... <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.

Keith Thompson

5/16/2011 8:05:00 PM

0

Seebs <usenet-nospam@seebs.net> writes:
> On 2011-05-16, Keith Thompson <kst-u@mib.org> wrote:
>> Keith Thompson <kst-u@mib.org> 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) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

lawrence.jones

5/16/2011 9:29:00 PM

0

Keith Thompson <kst-u@mib.org> wrote:
> Seebs <usenet-nospam@seebs.net> 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