Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String literals in C++

Posted on 2004-09-27
3
Medium Priority
?
592 Views
Last Modified: 2008-01-09
The following program seg faults on my machine.

int main() {
    char *cp = "Hell";
    *cp = 'B';
}

while the following works fine.

int main() {
    char cp[] = "Hell";
    *cp = 'B';
}

The following program does not compile.

int main() {
    const char *cp = "Hell";
    *cp = 'B';
}

Can someone explain the three observations?
0
Comment
Question by:priyendra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 12159234
1) cp points to read-only memory where program string constants are kept. Changing of this memory is access violation.
2) cp points to char aray allocated on stack. It is possible to change this memory.
3) *cp is constant pointer, *cp = ... is not allowed.
0
 

Author Comment

by:priyendra
ID: 12159385
Alex, thanks a lot for your answer. I guess this must be one of the fastest answers I have ever obtained in EE :-) For my other question, I shall await a few more comments before closing.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12162049
FYI:
Not all compilers will put string literals in read-only memory.

Changing the contents of a string literal results in undefined behavior IAW with the C++ standards.

Some compilers will allow you to do this, and others will not.
For example, VC++ 5.0 will allow you to change the contents of a pointer to a string literal.
However, VC++ 6.0/7.x will not.


You should avoid code that results in undefined behavior.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question