首页 > 解决方案 > 更改字符指针中的值会导致空白或问号

问题描述

我正在尝试学习 C 编程。目前我正在尝试了解不同字符类型的变量。例如:char var VS char* var VS const char var VS const* char var 等等。这是我想出的代码:

char* char_A[2] = {'A'};
const char* char_AA[2] = {'AA'};
char* const char_AAA[50] = {'AAA'};
const char* const char_AAAA[50] = {'AAAA'};

printf("\n\n"
       "char_A: %c\n"
       "char_AA: %c\n"
       "char_AAA: %c\n"
       "char_AAAA: %c\n"
       " \n\n"
       , char_A, char_AA, char_AAA, char_AAAA);

char_A[0] = 'B';
char_AA[0] = 'CD';
// char_AAA = 'Changed char_AAA'; // ILLEGAL, reason: constant content, movable pointer
// char_AAAA = 'Changed char_AAAA'; // ILLEGAL, reason: constant content and pointer!

printf("\n\n"
       "char_A: %c\n"
       "char_AA: %c\n"
       "char_AAA: %c\n"
       "char_AAAA: %c\n"
       " \n\n"
        , char_A, char_AA, char_AAA, char_AAAA);

//*char_AAA = char_A; // allowed because only the content cannot be changed (does not work with char array but works with other format, I believe)
// *char_AAAA = &char_AA; // ILLEGAL, reason: constant content and pointer!


printf("\n\n"
       "char_A: %c\n"
       "char_AA: %c\n"
       "char_AAA: %c\n"
       "char_AAAA: %c\n"
       " \n\n"
        , char_A, char_AA, char_AAA, char_AAAA);

为什么我会得到以下输出?:

char_A: 
char_AA: �
char_AAA: P
char_AAAA: �




char_A: 
char_AA: �
char_AAA: P
char_AAAA: �




char_A: 
char_AA: �
char_AAA: P
char_AAAA: �

我之前以以下方式定义了我的 char 变量:char* char_A = 'A';但也没有产生我预期的输出。我无法更改 char 变量的值,所以我尝试使用 char 数组,如您所见,它并没有按我的计划进行......

tl;博士为什么我会得到这个奇怪的输出,为什么我的 char 变量不会像我想要的那样改变?

编辑:我正在使用 CLion 编译我的程序,它使用 CMake 进行编译。没有错误并且运行良好。然而,当我用 gcc 编译程序时,我得到了错误,正如评论所指出的那样。有人愿意说明所有这些类型的不同之处吗?我只能找到比较两个甚至三个的教程和答案,但不是全部。我把从多个网站获得的这个小信息图放在一起:

char* vs const char* vs char* const vs const char* const

标签: cpointerscharacter

解决方案


“有人愿意说明所有这些类型有何不同吗?” 好的,很好,这里有很多内容要介绍,这就是为什么没有人回答的原因。

// This is a char, you can initialise it with _one_ letter in single quotes
char c = 'a';

// We are allowed to change this char
c = 'b';

// This char is const, so we are not allowed to change it
const char const_c = 'c';

const_c = 'd';    // Compiler error

// This is a pointer to a char, it holds an address
// Initialise it to the address of c
char *p_char = &c;

// We can change c via p_char
*p_char = 'e';

// We can't point to const_c though
p_char = &const_c;     // Compiler error

// We need a const char * for that
const char *p_const_char = &const_c;

// Here, the const can come before or after the char, it means the same thing
char const *p_const_char2 = &const_c;

// Either way we can't change the original char via these pointers
*p_const_char = 'f';      // Compiler error
*p_const_char2 = 'g';     // Compiler error

// We can still point to a char that isn't const
p_const_char = &c;

// But are still not allowed to change it via the pointer
*p_const_char = 'h';      // Compiler error

// With printf you use %c to print a single char
printf("%c %c %c %c", c, const_c, *p_char, *p_const_char);


// You can also put chars in an array
char array[3] = { 'i', 'j', 'k' };
printf("%c %c %c", array[0], array[1], array[2]);

// We can also use pointer syntax to access the array
printf("%c %c %c", *array, *(array+1), *(array+2));

// If the array is null terminated, it contains what is known as a string in C
char string[4] = { 'i', 'j', 'k', '\0' };

// Now we can use functions from string.h, strlen give the string length
printf("%uz", strlen(string));

// Use %s to print a string with printf
printf("%s", string);

// Char arrays can be initialised with string literals which use double quotes
// The \0 is added automatically
char string2[4] = "lmn";

// But be careful, the \0 won't get added if there is not enough space
char string3[3] = "opq";

// So now strlen doesn't know when to stop, this is bad
printf("%uz", strlen(string3));

// Usually better to not specify a size and let the compiler work it out
char string4[] = "rst";

// We can point to a string literal on it's own without an array
// Technically the string literal itself should be const, so we need a const
// pointer. Although lot of compilers are lax here
const char *p_const_string = "uvw";

char *p_string = "xyz";    // Might be allowed, really shouldn't though

// Are allowed to point to something that isn't const
p_string = string4;

// We can increment the pointer to point to the next character in the string
++p_const_string;
printf("%c", *p_const_string);   // prints v

// This generally more convenient than indexes when looping over a string
while('\0' != *p_string)
    printf("%c", *p_string++);

// But if we put a const after the *, we are not allowed to change the pointer
char *const const_p_string = string4;

++const_p_string;    // Compiler error

// If we put the const in both places, we can't change what we point to
// nor the pointer itself
const char *const const_p_const_string = string4;

++const_p_const_string;        // Compiler error
*const_p_const_string = 'a';   // Compiler error

认为这涵盖了大部分内容。注意字符串和字符序列之间的差异。两者都使用相同的类型,并且它们可能位于 char 数组中(或内存中实际上不属于数组的其他位置)。唯一的区别是\0最后,如果它没有它并且你把它当作一个字符串,你的程序可能会崩溃。


推荐阅读