By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 991 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

Waring for string is absent.

P: n/a
void foo(char * str)
{
str[0] = str[0];
}

int main()
{

const char str[] = "Hello";
foo(str); // i get warning here
foo("shaan"); // i dont get warning here.

}

should i interpret above programme as, String returns pointer to char
(which is non-constant).
Above statement is correct ?

Aug 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
shaanxxx wrote:
void foo(char * str)
{
str[0] = str[0];
}

int main()
{

const char str[] = "Hello";
foo(str); // i get warning here
foo("shaan"); // i dont get warning here.

}
You get a warning on the first call to foo because
str (in main) is a `const' string, but the argument of
foo is not `const'. "Adding" a restriction is harmless,
but "subtracting" a restriction generates a diagnostic.

You do not get a warning on the second call because
the literal "shaan" generates a string that is not `const'.
That is an accident of history, having to do with the way
C developed and was used before the `const' keyword came
into the language. The peculiar outcome is that a string
constant is not `const', but you must act as if it were.

Both calls to foo invoke undefined behavior by trying
to modify the string:

- In the first case the string is actually `const' and
it is undefined behavior to attempt to modify a
`const' object.

- In the second case the string is not `const', but it
is undefined behavior to attempt to modify a string
constant.

And yes: Replacing a character with itself is in fact an
attempt to "modify" the string, even though the modification
(if it succeeded) would give the string the same content as
it had before.
should i interpret above programme as, String returns pointer to char
(which is non-constant).
Above statement is correct ?
Sorry; I do not understand this.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 26 '06 #2

P: n/a

Eric Sosman wrote:
>
Sorry; I do not understand this.
I got the answer. I wanted to ask, why 'string' is not define as const.
Thanks,
Shaan.

Aug 26 '06 #3

P: n/a
shaanxxx wrote:
Eric Sosman wrote:
> Sorry; I do not understand this.
I got the answer. I wanted to ask, why 'string' is not define as const.
'string' in C refers to a particular layout in memory: a contiguous
block of 'char' values, ending with a zero value. The zero value is
called a null character.

You can have const strings and non-const strings. More precisely, you
can declare an array to hold const data or non-const data, and you can
access a string through a 'pointer to const char' or a 'pointer to
non-const char'.

foo is an array of const char, containing a string.

char const foo[] = {'h', 'e', 'l', 'l', 'o', 0};

bar is an array of non-const char, containing a string.

char bar[] = {'w', 'o', 'r', 'l', 'd', 0};

You may modify the contents of foo, but not bar.

--
Simon.
Aug 26 '06 #4

P: n/a
Simon Biber wrote:
shaanxxx wrote:
>Eric Sosman wrote:
>> Sorry; I do not understand this.
I got the answer. I wanted to ask, why 'string' is not define as const.


'string' in C refers to a particular layout in memory: a contiguous
block of 'char' values, ending with a zero value. The zero value is
called a null character.

You can have const strings and non-const strings. More precisely, you
can declare an array to hold const data or non-const data, and you can
access a string through a 'pointer to const char' or a 'pointer to
non-const char'.

foo is an array of const char, containing a string.

char const foo[] = {'h', 'e', 'l', 'l', 'o', 0};

bar is an array of non-const char, containing a string.

char bar[] = {'w', 'o', 'r', 'l', 'd', 0};

You may modify the contents of foo, but not bar.
I think you mean "bar, but not foo."

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 26 '06 #5

P: n/a
Eric Sosman wrote:
Simon Biber wrote:
>foo is an array of const char, containing a string.

char const foo[] = {'h', 'e', 'l', 'l', 'o', 0};

bar is an array of non-const char, containing a string.

char bar[] = {'w', 'o', 'r', 'l', 'd', 0};

You may modify the contents of foo, but not bar.

I think you mean "bar, but not foo."
Indeed.

--
Simon.
Aug 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.