• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

why does this simple c++ code crash

quirky

Senior member
can soembody please tell me why this simple C++ code compiles but does not execute? thanks, i rilly hate having questionsgo unanswered

#include <iostream.h>
#include <iomanip.h>

main(){

char const *ptr = "porcupine";
char *const ptr2= "por";

cout << ptr << '\n';
cout << *ptr << '\n';
cout << ptr2 << '\n';
*ptr2 = 'h';
cout << *ptr2 << '\n';

return 0;


}
 
If I remember right, does C++ not support "ptr2 = 'h';" in that format? Doesn't it need := or something? Might be way off; been awhile since I've done programming.
 
Originally posted by: Jeff7
If I remember right, does C++ not support "ptr2 = 'h';" in that format? Doesn't it need := or something? Might be way off; been awhile since I've done programming.

No, you got yourself confused with Pascal syntax 🙂

quirky, I am not sure if this is what's causing the problem. In your main function, you return the value 0 but your main function is not declared to return anything. You either, change it to:

int main() or

void main() and remove the return 0; statement.

As I said, it could be something else.. but that's what I noticed at my first glance at the code. Kinda surprised that your compiler didn't complain about that.
 
It is with that assignment, although I'm not sure why off hand.

Is there a reason you're not using the STL string types or even str* C functions?

porcupine
p
por

Program received signal SIGSEGV, Segmentation fault.
main () at test.cpp:12
12 *ptr2 = 'h';
 
Originally posted by: EagleKeeper
Are you able to modify a const?
I know the question is rhetorical, but just to be explicit, of course not. 🙂

The other problem here is that const in conjunction with pointers is tricky (many folks like to read the pointer type declaration right to left). The assignment obviously fails because ptr2 is a constant pointer. But what's less obvious is that string literals are implicitly constant char arrays, and they cannot be modified either.

So this is okay:
ptr = "some other string literal";

but this is always undefined regardless of const modifiers:
*ptr = 'x';

This question seems to crop up every couple months, so probably could go into a programming FAQ some folks were interested in cooking up.

Edit:
Just to clarify, C strings are not immutable. The simplest way to "fix" the above problem is to declare ptr2 as:
char ptr[ ] = "now I can modify this buffer";
 
Let me take a shot at it 🙂 Let's reduce the problem down to the simplest level:

Suppose we defined:
char *pStr = "Hello";

What happens when you compile? The string "Hello" is stored along with the program. When the OS loads the program image, the string "Hello" is loaded along with it. Now suppose you try to write something:

*pStr = 'X';


This will fail because pStr is pointing to the program data area which is read only. You cannot (usually) write to it. Thus if you try to write to it, an exception occurs (which you can catch by the way). So, essentially, the problem is not the pointer, but rather the constant string that is read only.
 
Have tried this problem on 3 different compilers.

Borland, VC6 and a Unix.

All compile, all choke.

Singh's explanation is correct all.

const of the pointer locks in the valud or the pointer (address of the string);

Books are sometimes wrong, the code is written to be compiled, and never executed.
This is not a failure of the C++ standard as the compiler digests the code.
It is a fault of the programmer trying to bypass the language rules.
 
Actually kt is right. You should just be able to change it to int main() and it should compile and run correctly.

Remember to read from left to right. Ptr is a pointer to a constant char(s). Ptr2 is a constant pointer to a char.

You can change the char(s) ptr2 points to because ptr2 is a constant pointer. The stuff it's pointing to isn't constant so you can change it.

Ptr on the other hand is a pointer to constant char(s). You can't change char(s) that Ptr is pointing too.

If it still doesn't compile try changing the line *ptr2 = 'h'; to *(ptr2) = 'h';

It could just be your compiler.

EJ
 
Back
Top