gpt4 book ai didi

更改字符指针中的值会导致空白或问号

转载 作者:行者123 更新时间:2023-11-30 20:17:20 27 4
gpt4 key购买 nike

我正在尝试学习 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;dr 为什么我会得到这个奇怪的输出,为什么我的 char 变量不会按照我想要的方式更改?

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

char* 与 const char* 与 char* const 与 const char* const

  • char* 表示指针和内容(字符/字符串)都可以更改

  • const char* 表示指针可以改变,但内容不能改变

  • char* const 表示指针不能改变,但内容可以改变

  • const char* const 表示指针和内容都不能更改

最佳答案

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

// 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 ,如果没有它并且您将其视为字符串,您的程序可能会崩溃。

关于更改字符指针中的值会导致空白或问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58041543/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com