dimanche 29 mars 2015

error: cannot increment value of type 'char [6]'


I am trying to learn pointers and string literals in C/C++. As per my understanding, string literals are char arrays with a null \0 at the end. Also we can basically do all the pointer arithmetic operations on array like increment and decrement.


But when I am trying to run the following code:



#include <iostream>
using namespace std;

int main (){
char *c ="Hello";
char d[6];

while(*d++ = *c++);

cout<<c<<endl<<d;
}


I am getting the following error,



error: cannot increment value of type 'char [6]'
while(*d++ = *c++);


My assumption for this code was, that the values of string literal c will be copied to char array d.




Aucun commentaire:

Enregistrer un commentaire