0

The nearest question on this website to the one I have had a few answers that didn't satisfy me.

Basically, I have 2 related questions :

Q. If I do something like :

char a[]= "abcde"; //created a string with 'a' as the array name/pointer to access it.

a[0]='z';  //works and changes the first character to z.

But

char *a="abcde";
a[0]='z'; //run-time error.

What is the difference? There is no "const" declaration, so I should be free to change contents, right?

Q. If I do something like :

int i[3];
i[0]=10; i[1]=20; i[2]=30;
cout<<*++i;  //'i' is a pointer to i[0], so I'm incrementing it and want to print 20.

This gives me a compile-time error, and I don't understand why.

On the other hand, this works :

int *i=new int[3];
i[0]=10; i[1]=20; i[2]=30;
cout<<*++i;  //Prints 20.

Thanks for the help.

3
  • 1
    Not needing const is a special rule that was dropped in C++11. That still doesn't mean it's modifiable. Commented Oct 27, 2013 at 20:55
  • 3
    There must be hundreds of duplicates of this question... Commented Oct 27, 2013 at 21:00
  • After incrementing i in the second half of the second question, you no longer have a pointer that can be passed to delete[]. Commented Oct 27, 2013 at 22:39

2 Answers 2

1

Q

char *a="abcde";
a[0]='z'; //run-time error.

Ans - Here a is pointing to string literal stored in read only location. You cannot modify string literal

Q

int i[3];
i[0]=10; i[1]=20; i[2]=30;
cout<<*++i; 

Ans- Array and Pointers are not same thing. Here i is not a pointer. You need lvalue for increment operand

You can do :

int *p = &i[0];
std::cout<<*++p;

In last case operator new returns a pointer to a allocated memory space, so its possible.

Sign up to request clarification or add additional context in comments.

Comments

1

Question 1

The trouble is that it is still const.

A string literal actually has the type char const*.
But when they designed C++03 they decided (unfortunately) that conversion from char const* to char* is not an error. Thus the code:

char *a="abcde";

Actually compiles without error (even though the string is const). BUT the string literal is still a const even though it is being pointed at via a non const pointer. Thus making it very dangerous.

The good news is that most compilers will generate a warning:

warning: deprecated conversion from string constant to ‘char*’

Question 2

cout<<*++i;  //'i' is a pointer to i[0], so I'm incrementing it and want to print 20.

At this point i is not a pointer. It's type is still an array.
It is not a valid operation to increment array.

The thing you are thinking about; is that arrays decay into pointers at the drop of a hat (like when you pass them to functions). Unfortunately that is not happening in this situation and thus you are trying to increment an array (which does not make sense).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.