Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: I need help

Reply
Thread Tools

Re: I need help

 
 
janus
Guest
Posts: n/a
 
      05-16-2011
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 =(
 
Reply With Quote
 
 
 
 
Shao Miller
Guest
Posts: n/a
 
      05-16-2011
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.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2011
Shao Miller <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2011
Keith Thompson <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      05-16-2011
On 2011-05-16, Keith Thompson <(E-Mail Removed)> wrote:
> Keith Thompson <(E-Mail Removed)> 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 / (E-Mail Removed)
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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2011
Seebs <(E-Mail Removed)> writes:
> On 2011-05-16, Keith Thompson <(E-Mail Removed)> wrote:
>> Keith Thompson <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      05-16-2011
Keith Thompson <(E-Mail Removed)> wrote:
> Seebs <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help, I am intermediate in Java...need help in follow case ElementX Java 9 10-01-2008 08:02 PM
Help Help. I really need some help with this =?Utf-8?B?Q2hyaXM=?= ASP .Net 3 01-31-2007 09:33 PM
re_---need help Network Adapters!!!! NEED HELP!!!! hedayatniac@gmail.com Computer Support 4 08-13-2006 01:03 AM
Need help! I need to add lead zeros to a textbox Teep ASP .Net 2 06-21-2004 01:04 PM
Please help!!! Need datagrid selection to fill textboxes...Need quick!! TN Bella ASP .Net 1 06-18-2004 01:31 AM



Advertisments