我使用cmd来编译.bat和g ++.exe.谁能告诉我我做错了什么?我正在使用它来练习,因为我遵循关于字符串和数组的教程.请记住,我还在学习.
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 . . .
Run Code Online (Sandbox Code Playgroud)
我的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
小智 7
双引号之间的字符串是字符串文字.它们的数组char
不被修改(尝试修改它们会调用未定义的行为).这就是为什么你应该声明指向字符串文字的指针const char *
- 这样,如果某些代码错误地尝试在文字中写入/修改字符,编译器将有机会捕获此错误.
归档时间: |
|
查看次数: |
10941 次 |
最近记录: |