You are here:

C++/problem with pointer and string constant

Advertisement


Question
main(){
char *ptr="hello world";
char arr[]="hello world";
*(ptr+1)='x';//it does not work
 *(arr+1)='x'; //but it works why??
}

Answer
Note: you are asking this in a C++ section so I am answering assuming you are writing C++. If you are writing C then please ask an expert in the C language section.

In fact it is possible neither will do what you require. String literals have type array of n const char. Note that const specification in there - the characters of the string literal are CONSTANT meaning YOU SHOULD NOT CHANGE THEM. I do not usually 'shout' but this point bears emphasising. Usually const means you cannot assign it to a less qualified type - i.e. a type associated to non const char. However there is an exception for narrow (char) string literals so as to enhance C++ compatibility with C. This language feature is depreciated by the way.

One reason you are likely to get into trouble is because the compiler and/or linker are free to share string literal memory to save space - so all literal strings of "hello world" could refer to a single copy. Or they might not. It depends on the compiler, linker and the options and setting used. This point is made explicitly in the C++ standard, section 2.13.4 paragraph 2 reads:

       "2 Whether all string literals are distinct
         (that is, are stored in non-overlapping
         objects) is implementation-defined. The
         effect of attempting to modify a string
         literal is undefined."

So what happens when you attempt to modify a string literal is undefined - that is anything could happen from not doing what you expect, crashing, or even formatting your hard disk (unlikely though allowed by the standard!).

By the way the code compiled under both MS VC++ 8 (2005) on Windows XP and g++ 4.0.2 on SuSE Linux 10.0, note however that the definition of main should be either:

       int main() { /* ... */ }

or

       int main(int argc, char* argv[]) { /* ... */ }

Hope this helps.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.