我已经阅读了很多关于它的线索和问题并阅读了很多答案,但仍然难以理解差异以及何时应该使用什么?
我认为你应该在需要存储数据时使用char *而你不知道它的大小,因为它是动态的。 另外我不确定我是对的,但是如果你声明一个char *并且给它分配一个像这样的文字文本,我不明白我的理解:char * ch =“hi”; 它是一个你无法改变的常量,如果你试图改变它,你只需将ch指向另一个分配的存储空间来保存新字符串? 如果这样写:char ch = malloc(20); 然后你可以改变价值,如果你这样做:char ch [] =“hi”; char pch = ch; 您可以更改值,因为您指向数组并且数组指向ch [0]?
所有用粗体写的都是我从阅读中理解的,虽然我可能对我刚才说的大部分内容都是错的,这就是为什么我需要一个非常好的简单解释,所以我可以一劳永逸地理解差异,当我应该用什么。
编辑:
#include main() { char ch[] = "Hello"; char *p1 = ch; char *p2 = p1; char *p3 = *p1; printf("ch : %sn", ch); printf("p1 address [%d] value is %sn", p1, *p1); printf("p2 address [%d] value is %sn", p2, *p2); printf("p3 address [%d] value is %sn", p3, *p3); return 0; }
最直截了当的答案是:
这里的区别在于
char *s = "Hello world";
将Hello世界放在内存的只读部分并制作sa指针,使得对此内存的任何写入操作都是非法的。 做的时候:
char s[] = "Hello world";
将文字字符串放在只读内存中,并将字符串复制到堆栈上新分配的内存中。 从而制作
s[0] = 'J';
法律。
更长的解释将包括存储器存储的段,以及分配的内存量:
Example: Allocation Type: Read/Write: Storage Location: Memory Used (Bytes): =========================================================================================================== const char* str = "Stack"; Static Read-only Code segment 6 (5 chars plus ' ') char* str = "Stack"; Static Read-only Code segment 6 (5 chars plus ' ') char* str = malloc(...); Dynamic Read-write Heap Amount passed to malloc char str[] = "Stack"; Static Read-write Stack 6 (5 chars plus ' ') char strGlobal[10] = "Global"; Static Read-write Data Segment (R/W) 10
参考
编辑
为了解决问题中的编辑和随之发布的评论,我在您的解决方案中添加了注释:
#include int main() { char ch[] = "Hello"; /* OK; Creating an array of 6 bytes to store * 'H', 'e', 'l', 'l', 'o', ' ' */ char *p1 = ch; /* OK; Creating a pointer that points to the * "Hello" string. */ char *p2 = p1; /* OK; Creating a second pointer that also * points to the "Hello" string. */ char *p3 = *p1; /* BAD; You are assigning an actual character * (*p1) to a pointer-to-char variable (p3); * It might be more intuitive if written in * 2 lines: * char* p3; * p3 = *p1; //BAD */ printf("ch : %sn", ch); /* OK */ printf("p1 address [%d] value is %sn", p1, *p1); /* Bad format specifiers */ printf("p2 address [%d] value is %sn", p2, *p2); /* Bad format specifiers */ printf("p3 address [%d] value is %sn", p3, *p3); /* Bad format specifiers */ return 0; }
所以,三个主要的错误。
例子
#include int main(void) { char c = 'H'; // A character char* pC = &c; // A pointer to a single character; IS NOT A STRING char cArray[] = { 'H', 'e', 'l', 'l', 'o' }; // An array of characters; IS NOT A STRING char cString[] = { 'H', 'e', 'l', 'l', 'o', ' ' }; // An array of characters with a trailing NULL charcter; THIS IS A C-STYLE STRING // You could also replace the ' ' with 0 or NULL, ie: //char cString[] = { 'H', 'e', 'l', 'l', 'o', (char)0 }; //char cString[] = { 'H', 'e', 'l', 'l', 'o', NULL }; const char* myString = "Hello world!"; // A C-style string; the ' ' is added automatically for you printf("%sn", myString); // OK; Prints a string stored in a variable printf("%sn", "Ducks rock!"); // OK; Prints a string LITERAL; Notice the use of DOUBLE quotes, " " printf("%sn", cString); // OK; Prints a string stored in a variable printf("%cn", c); // OK; Prints a character printf("%cn", *pC); // OK; Prints a character stored in the location that pC points to printf("%cn", 'J'); // OK; Prints a character LITERAL; Notice the use of SINGLE quotes, ' ' /* The following are wrong, and your compiler should be spitting out warnings or even not allowing the * code to compile. They will almost certainly cause a segmentation fault. Uncomment them if you * want to see for yourself by removing the "#if 0" and "#endif" statements. */ #if 0 printf("%sn", c); // WRONG; Is attempting to print a character as a string, similar // to what you are doing. printf("%sn", *pC); // WRONG; Is attempting to print a character as a string. This is // EXACTLY what you are doing. printf("%sn", cArray); // WRONG; cArray is a character ARRAY, not a C-style string, which is just // a character array with the ' ' character at the end; printf // will continue printing whatever follows the end of the string (ie: // random memory, junk, etc) until it encounters a zero stored in memory. #endif return 0; }
代码清单 – 提议的解决方案
#include int main() { char ch[] = "Hello"; /* OK; Creating an array of 6 bytes to store * 'H', 'e', 'l', 'l', 'o', ' ' */ char *p1 = ch; /* OK; Creating a pointer that points to the * "Hello" string. */ char *p2 = p1; /* OK; Creating a second pointer that also * points to the "Hello" string. */ char *p3 = p1; /* OK; Assigning a pointer-to-char to a * pointer-to-char variables. */ printf("ch : %sn", ch); /* OK */ printf("p1 address [%p] value is %sn", p1, p1); /* Fixed format specifiers */ printf("p2 address [%p] value is %sn", p2, p2); /* Fixed format specifiers */ printf("p3 address [%p] value is %sn", p3, p3); /* Fixed format specifiers */ return 0; }
样本输出
需要了解更多c/c++开发分享char *和char 之间的区别,也可以关注C/ C++技术分享栏目—计算机技术网(www.ctvol.com)!
ch : Hello p1 address [0x7fff58e45666] value is Hello p2 address [0x7fff58e45666] value is Hello p3 address [0x7fff58e45666] value is Hello
以上就是c/c++开发分享char *和char 之间的区别相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/980632.html