Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用字符指针和字符数组之间的区别_C++_C_Pointers_Character - Fatal编程技术网

C++ 使用字符指针和字符数组之间的区别

C++ 使用字符指针和字符数组之间的区别,c++,c,pointers,character,C++,C,Pointers,Character,基本问题 char new_str[]=""; char * newstr; 如果我必须将一些数据连接到其中,或者使用strcat/substr/strcpy之类的字符串函数,那么两者之间的区别是什么 我知道我必须为char*方法分配内存(第2行)。但我不确定怎么做 常量字符*和字符串文字是一样的吗 我需要更多的了解。有人能指出一些很好的详尽的内容/材料吗?请仔细阅读以下内容: 另请参见char数组,如您的示例中的char new_str[],然后新的_str将始终指向数组的底部。指针本身不

基本问题

char new_str[]="";

char * newstr;
如果我必须将一些数据连接到其中,或者使用strcat/substr/strcpy之类的字符串函数,那么两者之间的区别是什么

我知道我必须为char*方法分配内存(第2行)。但我不确定怎么做

常量字符*和字符串文字是一样的吗

我需要更多的了解。有人能指出一些很好的详尽的内容/材料吗?

请仔细阅读以下内容:

另请参见char数组,如您的示例中的char new_str[],然后新的_str将始终指向数组的底部。指针本身不能递增。是的,您可以使用下标访问数组中的下一个字符,例如:
new_str[3]

但是如果指针指向char,指针可以递增
new\u str++
,以获取数组中的下一个字符


另外,为了更加清晰,我建议您使用。

区别在于一个是指针,另一个是数组。例如,可以使用sizeof()数组。你可能对偷看感兴趣,第一种是char[1],第二种是char*。不同类型

在C中使用代码< MalOC/分配内存,或者在C++中使用< <代码> < <代码> >

char foo[] = "Bar";  // Allocates 4 bytes and fills them with
                     // 'B', 'a', 'r', '\0'.
此处的大小由初始值设定项字符串暗示

foo
的内容是可变的。您可以更改
foo[i]
,例如
i
=0..3

如果你这样做:

char *foo = "Bar";
编译器现在在只读内存中分配一个静态字符串“Bar”,并且不能修改

foo[i] = 'X';  // is now undefined.

如果你在C++中,为什么不使用你所有的字符串需求?尤其是处理连接的任何东西。这将为您节省很多问题。

< P>如果您使用的是C++,您的标签应该是使用C++字符串,而不是C<代码> char < /Cord>数组。
string
类型使操作字符串变得更加容易

如果由于某种原因而无法使用
char
数组,则行:

char new_str[] = "";
分配1字节的空间,并在其中放入一个空终止符字符。这与:

char *new_str = "";
因为这可能会给你一个不可写内存的引用。声明:

char *new_str;
它本身给你一个指针,但它没有指向任何东西。如果它是函数的局部值,它也可以有一个随机值

人们倾向于(用C而不是C++)做如下事情:

char *new_str = malloc (100); // (remember that this has to be freed) or
char new_str[100];
为了获得足够的空间


如果您使用
str..
函数,那么您基本上要负责确保
char
数组中有足够的空间,以免在调试代码时遇到各种奇怪而奇妙的练习。如果使用实C++字符串,很多繁琐的工作都是为你完成的。

< P>这是一个字符数组:

char  buf [1000];
例如,这毫无意义:

buf = &some_other_buf;
这是因为
buf
,尽管它具有类型指针的特性,但它已经指向了唯一对它有意义的地方

char *ptr;
另一方面,
ptr
只是一个指针,可能指向某个地方。通常是这样的:

ptr = buf;              // #1:  point to the beginning of buf, same as &buf[0]
或者可能是这样:

ptr = malloc (1000);    // #2:  allocate heap and point to it
或:

对于所有这些情况,*ptr都可以写入,但第三种情况除外,即某些编译环境将字符串常量定义为不可写入

*ptr++ = 'h';          // writes into #1: buf[0], #2: first byte of heap, or
                       //             #3 overwrites "a"
strcpy (ptr, "ello");  // finishes writing hello and adds a NUL

消除混淆的最佳来源是Peter Van der Linden,C编程专家,Deep C secrets——数组和指针在内存中的寻址方式不同

对于数组,地址(例如0x9876)在运行时使用的是间接寻址方案。假设newstr是malloc'd或

char new_str[]; newstr = malloc(10); 总之,两者之间的真正区别在于如何在内存中访问它们

拿着这本书,读着它,生活着它,呼吸着它。这是一本精彩的书!:)

这将指定大小为5字节的字符(字符串)数组(每个字符一个字节加上空终止符一个字节)。因此,它将字符串“abcd”存储在内存中,我们可以使用变量new_str访问该字符串

char *new_str="abcd";  

这将指定一个字符串“abcd”存储在内存中的某个位置,指针new_str指向该字符串的第一个字符。

要在内存分配端区分它们:

// With char array, "hello" is allocated on stack
char s[] = "hello";

// With char pointer, "hello" is stored in the read-only data segment in C++'s memory layout.
char *s = "hello";

// To allocate a string on heap, malloc 6 bytes, due to a NUL byte in the end
char *s = malloc(6);
s = "hello";

嗯,不。foo是不可变的。你不能改变foo。您可以更改foo的元素,是的,但是您不能将foo更改为bar,例如,如果bar也是一个char数组。作为旁注:注意C具有可交换表达式,例如4+3与3+4相同。在指针中也是如此,*(new_str+1)与*(1+new_str)相同,顺便说一句,它是1[new_str]。这是一个笑话,在网上其他地方被引用,可能是为了甩掉某人。它很少在生产中使用,还没有在某处看到它!看看这篇精彩的教程,现在两个链接都失效了:(
*(new_str + 1)
= *newstr;
*(new_str + 1)
= newstr[1];
char new_str[]="abcd";  
char *new_str="abcd";  
// With char array, "hello" is allocated on stack
char s[] = "hello";

// With char pointer, "hello" is stored in the read-only data segment in C++'s memory layout.
char *s = "hello";

// To allocate a string on heap, malloc 6 bytes, due to a NUL byte in the end
char *s = malloc(6);
s = "hello";