-1

This program is compiled and run but showing error: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

#include<iostream>
using namespace std;
struct person{
 char* name;
 char* lastName;
 int age;
};
void show(person temp){
 cout << temp.name << " " << temp.lastName << " age is: " << temp.age;
}
int main(){
// decaring a person    
 person p;
 p.name = "Jayant";
 p.lastName = "Sharma";
 p.age = 18;
//reassigning of p 'name'
 p.name = "Kartik";
 show(p);
 return 0;
}
// output => Kartik age is: 18

But when I declare variables using const compiler not shows an error and reassigning is also done

struct person{
    const char* name;
    const char* lastName;
    int age;
};

reassigning the name 'Kartik' to the same const variable 'name' is done without an error.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Jayant Sharma
  • 87
  • 1
  • 7
  • 1
    Because `"string literal"` is a `const char *`, and you can't assign a pointer to a const to a pointer to a non-const. C++ does not work this way, that's all. That's what the error message says, in plain words. Which part of the compiler's error message you're unsure about? – Sam Varshavchik Jun 11 '20 at 02:19
  • @SamVarshavchik technically, a string literal is a `const char[N]`, which *decays* into a `const char*` – Remy Lebeau Jun 11 '20 at 03:25

1 Answers1

0

I see your confusion about your sample source code. However, I see two questions you did not explicitly state, but implied. So I will answer both!

What are String Literals in C++?

The ISO/IEC C++ standard defines a string literal as an array of const char * , and your sample code violates this definition by declaring your variables name and lastName as having type char * only! Although this isn't the case in the C language see Why is conversion from string constant to 'char*' valid in C but invalid in C++ for more details!

Way are Variables Assigned Even When Declared Const?

The compiler doesn't throw an error at you because you did not utilize the const keyword correctly!

const char* name = "Sharma";

The above code sample states that the value held in variable name is constant, but its address is not!

const char* const name = "Sharma"; 

Now the above code sample states that both the value and address held in variable name is constant!

When you use the member of object operator you are stating that the memory address being manipulated is the address of the object being accessed plus the offset where the data is stored. Therefore, you are indirectly modifying your variables via an address!

  • Ok I got your point. Address remain same but value isn't. That's what you want to say... – Jayant Sharma Jun 11 '20 at 04:09
  • Was I being to descriptive? If so, sorry about that. I remember asking this same question a long time ago. Finding out that _member of object_ operator returns an __address__ and not an actual __value__ was very surprising time me! –  Jun 11 '20 at 04:50
  • Not at all your description made it easy for me to conclude about that. So. don't worry about tha. – Jayant Sharma Jun 11 '20 at 04:53