2

I use the cmd to compile with a .bat and g++.exe. Can someone tell me what I did wrong? I am using it to practice as I follow a tutorial on strings and arrays. Please keep in mind that I am still learning.

main.cpp: In function 'int main()':
main.cpp:6:12: warning: deprecated conversion from string constant to 'char*' [-
Wwrite-strings]
main.cpp:10:12: warning: deprecated conversion from string constant to 'char*' [
-Wwrite-strings]
main.cpp:11:12: warning: deprecated conversion from string constant to 'char*' [
-Wwrite-strings]
Press any key to continue . . .

My Code:

      using namespace std;
#include <iostream>

int main() {
//Asterisk to make the variable an array. Takes 8 bytes of memory (8 characters including spaces).
 char *a = "hi there";
//Inside square brackets is the number of bytes of memory to use. More consumtion of resources
 char b[500]="hi there";
 //For a new line, type \n. For a tab, type \t.
 char *c = "Hi There\nFriends!";
 char *d = "\t\tHi There Friends!";
 //endl will end the line.
 cout << a;
 cout << b << endl;
 cout << c << endl;
 cout << d << endl;
}
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
Henry
  • 29
  • 1
  • 1
  • 2

1 Answers1

7

Strings between double quotes are string literals. They are arrays of char which are not to be modified (attempting to modify them invokes undefined behavior). That's why you should declare a pointer pointing to a string literal as const char * - this way, if some code erroneously tries to write/modify a character in the literal, the compiler will have a chance to catch this error.