473,770 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1536
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 "subtractin g" 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
20455
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 8KB at a time if not data: break which leaves me with data as a string obj. The problem that I have is that the function that processes the archive expects a file object. So far the only solution I have found it to write the file to disk and then
2
2496
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 target namespace of the components from the included schema document. I'm confused because of the rules I read in the XML Schema spec <http://www.w3.org/TR/xmlschema-1/#element-element>: > If the <element> element information item has <schema>...
2
1725
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 participate in USENET...get rewarded for it!
9
8702
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 knowledgeable insight into this. My class Position has a string (private, public - doesn't seem to matter which), which I construct in a member function and wish to return calls from main(). However, although
0
1549
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 "" What am I doing wrong ? (GIVE ME A HAND!) bool vfpColorSet::LoadFile(const char* file) {
1
1510
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 Detail_Format(Cancel As Integer, FormatCount As Integer)
10
14999
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 A compact form of it can as well be without the minuses and
20
691
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 example will produce an error as we are trying to modify a constant. So, str1 is not a string constant in C++.
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10230
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10058
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10004
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9870
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.