473,405 Members | 2,294 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,405 software developers and data experts.

Is C-style string unchangable?

Dear All,

I hear a lot that cstring cannot be changed. But my code:

/************************************************** ***********
* Testing cstring(a pointer)
* ************************************************** ********/
#include <iostream>
using namespace std;

int main()
{
char shortsTring[] = "abc";
shortsTring[2] = 'X';

cout << shortsTring << endl; //print out abX !!!

return 0;
}

So why many people are saying cstring cannot be changed? Could you help
me? Thank you very much.

Dec 30 '05 #1
2 1388

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp**********@osf1.gmu.edu...
Dear All,

I hear a lot that cstring cannot be changed.

If by 'cstring' you mean the 'C-style string', i.e. a zero-terminated
array of characters, then you heard wrong. For various reasons, *some*
C-style strings are not modifiable in a defined way. To state this about
*any* such string is simply wrong.

What cannot be modified is an array whose elements are const:

const char arr[] = "Attempts to modify this string"
" should result in compiler diagnostic";

or a string literal:

char *p = "I am a literal";

p[0] = 'i'; // undefined behavior.
But my code:

/************************************************** ***********
* Testing cstring(a pointer)
There are no pointers declared or defined in your code.
* ************************************************** ********/
#include <iostream>
using namespace std;

int main()
{
char shortsTring[] = "abc";
This is an array of four type 'char' objects. It is not a pointer.
shortsTring[2] = 'X';
This replaces the object at shortsTring[2] with the character value 'X'
The string now contains: "abX"

cout << shortsTring << endl; //print out abX !!!
This is expected and normal behavior. You created a 'normal'
non-const (i.e. modifiable) array of characters, changed the
value of one of those characters, then inserted all the characters
into the output stream designated by 'cout'.

Which C++ book(s) are you reading?

return 0;
} -
So why many people are saying cstring cannot be changed?
Two possibilites:
1. They're simply wrong.
2. You've misunderstood.

Depending upon context, some objects are modifiable, others are not.
The types of these objects (such as char, or arrays thereof), makes
no difference.
Could you help me? Thank you very much.


The best help I can offer is to advise you to abandon the use
of character arrays for representing strings in C++, and use
the 'std::string' type (declared by <string>), which was created
specifically for the purpose.

-Mike
Dec 30 '05 #2
"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:dp**********@osf1.gmu.edu...
Dear All,

I hear a lot that cstring cannot be changed. But my code:

/************************************************** ***********
* Testing cstring(a pointer)
* ************************************************** ********/
#include <iostream>
using namespace std;

int main()
{
char shortsTring[] = "abc";
shortsTring[2] = 'X';

cout << shortsTring << endl; //print out abX !!!

return 0;
}

So why many people are saying cstring cannot be changed? Could you help
me? Thank you very much.


constant "cstring" (c style strings) can not (should not) be changed. It is
fine to change non constant c style arrays as you've seen.

Whoever told you that all c style strings can not be changed was simply
wrong. A C style string is simply an array of characters which is null (0)
terminated. Nothing more, nothing less. You can change the data just as
you can change any array data.

Note the difference between:
char* shortsTring = "abc"; // shortsTring points to constant data
and
char shortsTring[] = "abc"; // shortsTring is an array of non constant data


Jan 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
9
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
1
by: Thomas | last post by:
It looks like the String.replace doesn't work in IE6.1. Anyone else has the same problem. I am using newest service package of IE and Win2K. Thanks
22
by: Michael Rasmussen | last post by:
Hi all, Not sure if this is OT? I have a function in a library written in C++ which returns a strdup(s.c_str()) to an application written i C. Running Valgrind on my C-application shows this...
4
by: lyealain | last post by:
beginner here..hahahha... how can i change the textbox become unchangable ar>??? can see but cannot change...
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: 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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...

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.