Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Waring for string is absent.

Reply
Thread Tools

Waring for string is absent.

 
 
shaanxxx
Guest
Posts: n/a
 
      08-26-2006
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 ?

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-26-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid


 
Reply With Quote
 
 
 
 
shaanxxx
Guest
Posts: n/a
 
      08-26-2006

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.

 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      08-26-2006
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.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      08-26-2006
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
(E-Mail Removed)lid
 
Reply With Quote
 
Simon Biber
Guest
Posts: n/a
 
      08-26-2006
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.
 
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
waring in vhdl Xst:1355 chaitanya.531 VHDL 0 03-01-2012 12:31 PM
Waring about pointers kyle.tk C Programming 5 02-17-2006 01:42 AM
Virus Waring! Nowhere Computer Support 2 03-17-2005 10:38 PM
String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 3 12-05-2003 04:20 PM
Re: String.replaceAll(String regex, String replacement) question Mladen Adamovic Java 0 12-04-2003 04:40 PM



Advertisments