基础问题。
char new_str[]="";
char * newstr;
如果我需要将一些数据连接在一起或使用字符串函数(如strcat/substr/strcpy),这两种方法之间有什么区别?我知道我需要为char *方法分配内存(第2行)。但我不确定该怎么做。
const char *和字符串字面量是相同的吗?
我需要更多信息。有人可以指向一些详尽的内容/材料吗?
基础问题。
char new_str[]="";
char * newstr;
如果我需要将一些数据连接在一起或使用字符串函数(如strcat/substr/strcpy),这两种方法之间有什么区别?char new_str[];,编译器在编译和运行时都知道new_str的内存地址,例如0x1234,因此可以简单地使用
[]
进行索引。例如,new_str[4]
在运行时,代码会选择new_str所在的地址,例如0x1234(即物理内存中的地址),通过加上索引说明符[4]
, 0x1234 + 0x4,就可以检索到该值。char *newstr分配一个地址,例如0x9876,但在运行时,该地址是一种间接寻址方案。假设newstr被malloc'd
newstr = malloc(10);,每次在代码中使用newstr进行引用时,由于编译器知道newstr的地址即0x9876,但newstr指向的却是一个可变变量。在运行时,代码从物理内存0x9876(即newstr)中获取数据,但在该地址处是另一个内存地址(因为我们使用了malloc),例如0x8765,在这里,代码从malloc分配给newstr的内存地址0x8765中获取数据。
char new_str[]
和char *newstr
是可以互换使用的,因为数组的零元素索引会转化为指针,这解释了为什么你可以使用newstr [5]
或者*(newstr+5)
。注意,即使我们声明了char *newstr
,指针表达式也被使用,因此*(new_str+1)
=*newstr;
或者*(new_str+1)
=newstr[1];
总之,两者之间的实际区别在于它们在内存中的访问方式。
购买这本书,阅读它,体验它,并且深入理解它。这是一本绝妙的书! :)
这是一个字符数组:
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 = "abcdefghijklmn"; // #3: string constant
对于所有这些情况,*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
第一个的类型是char [1],第二个是char *。类型不同。
在C中使用malloc
或在C++中使用new
为后者分配内存。
char foo[] = "Bar"; // Allocates 4 bytes and fills them with
// 'B', 'a', 'r', '\0'.
这里的大小是从初始化字符串中推断而来的。
foo
的内容是可变的。例如,您可以更改 foo[i]
,其中 i
= 0..3。
另一方面,如果您这样做:
char *foo = "Bar";
foo[i] = 'X'; // is now undefined.
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++ 字符串,很多重活都为你完成了。
// 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";
char new_str[]="abcd";
这指定了一个大小为5个字节的字符数组(字符串)(每个字符一个字节加上一个空终止符)。因此,它将字符串“abcd”存储在内存中,我们可以使用变量new_str访问该字符串。
char *new_str="abcd";
这指定了一个字符串 'abcd' 存储在内存中的某个位置,而指针 new_str 指向该字符串的第一个字符。