Re: Doubt about array's name



nembo kid <user@xxxxxxxxxxxxx> writes:
Richard Heathfield ha scritto:
Why not? It's only a copy, after all. It doesn't affect the original
array address in any way whatsoever.

Ok my doubts cleared all.

But anyway I dont'understand because someone add the qualificator
"const" to string array passed to the function (so to avoid any change
of this pointer).
/* Code */
void chartobyte (const char *s)
/* Code */

``const char *s'' declares s as a pointer to const char, not as a
const pointer to char. In other words, you're permitted to modify the
pointer (since it's just a local copy), but you're not permitted to
modify what it points to.

If you wanted to declare a const pointer to char (so the pointer
itself is read-only), you could write:

char *const s;

But function parameters, since they're purely local to the function,
are not typically declared as "const", since any modification won't
affect the caller anyway. You could declare a parameter as "const" if
you don't intend to modify it within the function.

--
Keith Thompson (The_Other_Keith) <kst-u@xxxxxxx>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: Is this legal ?
    ... >> to be followed again if the compiler can't guarantee that nothing that ... >address of an object into a pointer to const type version, ... to change it (by casting away const from a pointer to it, ...
    (comp.lang.c)
  • Re: API : constness ?
    ... >const char *const. ... You can declare a constant pointer (you can't change ... Declaring a parameter "const char *" is ONLY an help ...
    (comp.lang.python)
  • Re: Why is the kfree() argument const?
    ... is there any reason why kfreetakes a const pointer just to degrade it ... In the particular case of kfree(), the pointer argument *should* be const, ... If a struct has a constant pointer to it, ...
    (Linux-Kernel)
  • Re: When to use const modifier?
    ... My point wasn't how to declare, ... One should use const when the content won't be modified anywhere after ... It won't warn about a write to the pointer object itself, ... Sure there can be ways to chage it's data, but those are compiler ...
    (comp.lang.c)
  • Re: Array of char pointers.
    ... Like I said in the beginning, const is appropriate only if all the ... That snippet doesn't make a pointer to a string literal. ... char* is a subtype of const char*, ... as only the operations allowed on the supertype are safe. ...
    (microsoft.public.vc.language)