Waring for string is absent.

S

shaanxxx

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 ?
 
E

Eric Sosman

shaanxxx said:
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.
 
S

Simon Biber

shaanxxx said:
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.
 
E

Eric Sosman

Simon said:
shaanxxx said:
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."
 
S

Simon Biber

Eric said:
Simon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top