string and constant charaters

Summary

The bast way to declare string is the "character array" char is_a_string[] = "abc"; char is_a_string[10] = "abc"; This is a constant char array. char* is_a_const = "abc"

True or False

1
#include <stdio.h>
2
#include <string.h>
3
4
5
int main() {
6
7
//A) F
8
//The string "char* pc1" is a read-only literal (formally its type is const char[4]).
9
// -->char pc1[5] = "john";
10
// It's a CONST !!! You can't use char* to change constant.
11
char* pc1 = "john";
12
*pc1 = 'J';
13
14
//B) T
15
char pc2[] = "john";
16
*pc2 = 'J';
17
18
//C) F
19
//may use "restrict"
20
//"Hello, " is a "const char[]",but a "char array"
21
// It's a CONST !!! You can't use char* to change constant.
22
char* str1 = "Hello, ";
23
char* str2 = "world! ";
24
strcat(str1, str2);
25
26
//D) F
27
// char* pc3; --> error: ‘pc3’ is used uninitialized in this function
28
// Even if initialized pc3, It's a CONST, too !!!
29
// You can't use char* to change constant.
30
char* pc3;
31
*pc3 = 'a';
32
33
return 0;
34
}
Copied!
Copy link