473,385 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Waring for string is absent.

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
5 1517
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matthew Thorley | last post by:
I'm writing a web app whereby a user uploads a tar acrhive which is then opened and processed. My web form reads the file like this: while 1: data = value.file.read(1024 * 8) # Read blocks of...
2
by: Stanimir Stamenkov | last post by:
I'm trying to find out if it is permissible to include a schema document with absent target namespace to a schema with specified target namespace, and if it is, what are the rules to resolve the...
2
by: lian ravago | last post by:
How to get the num of days absent using c# when start date and end date are the date format is like this 12/24/12 *** Sent via Developersdex http://www.developersdex.com *** Don't just...
9
by: David Carter-Hitchin | last post by:
Hi, I'm not very experienced with c++ so I'm sure this is something obvious that is easily solved, but for the life of me I can't seem to figure it out. I'd be very grateful for some...
0
by: summer911 | last post by:
Hi I tried the code below ( xmlParseFile) on my xml file ( see below ) but this gives me a warning message!(OS :redhat9.0) The warning message is :I/O waring :failed to load external extity "" ...
1
by: pooter | last post by:
In my report called rptStockFlow i have the products underlined with red if balance is not equal to in - out: Const conNormal = 400 Const conHeavy = 900 ' exra bold Private Sub...
10
by: Reiner Merz | last post by:
Hi, I'm looking for advice on how to parse a timestamp string according to the ISO 8601 specification. For those unfamiliar with the standard, here's an example: 2003-09-09T23:00:00Z...
20
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.