简体   繁体   English

在C中定义字符串的正确方法是什么?

[英]what is the correct way to define a string in C?

what is the correct way to define a string in C? 在C中定义字符串的正确方法是什么? using: 使用:

char string[10];

or 要么

char *string="???";

If I use an array, I can use any pointer to point to it and then manipulate it. 如果使用数组,则可以使用任何指针指向它,然后对其进行操作。 It seems like using the second one will cause trouble because we didn't allocate memory for that. 似乎使用第二个会引起麻烦,因为我们没有为此分配内存。 I am taught that array is just a pointer value, I thought these two are the same before. 我被告知数组只是一个指针值,我以为这两个以前是相同的。 Until I did something like string* = *XXXX , and realize it didn't work like a pointer. 直到我做了类似string* = *XXXX ,并意识到它不能像指针一样工作。

As @affenlehrer points out, how you "define" a string depends on how you want to use it. 正如@affenlehrer所指出的,如何“定义”字符串取决于您要如何使用它。 In reality, 'defining' a string in C really just amounts to putting it in quotes somewhere in your program. 实际上,在C中“定义”字符串实际上只不过是将其放在程序中的引号中。 You should probably read more about how memory works and is allocated in C, but if you write: 您可能应该阅读更多有关内存如何工作以及如何在C中分配内存的信息,但是如果您这样写:

char *ptr = "???"

What happens is that the compiler will take the string "???" 发生的情况是编译器将使用字符串“ ???” (which is really four bytes of data, three '?'s followed by one zero byte for the NUL terminator). (实际上是四个字节的数据,三个“?”后跟一个零字节,用于NUL终止符)。 It will insert that at some static place in your program (in something called the .bss segment ), and when your program starts running, the value of ptr will be initialized to point to that location in memory. 它将在程序的某个静态位置(在.bss段中 )插入该字符 ,并且在程序开始运行时,将初始化ptr的值以指向内存中的该位置。 This means you have a pointer to four bytes of memory, and if you try to write outside of those bytes, your program is doing something bad (and probably violating memory safety). 这意味着您有一个指向四个字节的内存的指针,并且如果尝试在这些字节之外进行写入,则您的程序正在做某些不好的事情(可能违反了内存安全性)。

On the other hand, if you write 另一方面,如果你写

char string[10];

Then this basically tells the compiler to go allocate some space in your program of 10 bytes, and make the variable 'string' point to it. 然后,这基本上告诉编译器在程序中分配10个字节的空间,并使变量“ string”指向它。 It depends where you put this: if you put it inside a function, then you will have a stack allocated buffer of 10 bytes. 这取决于将其放置在哪里:如果将其放置在函数中,则将为堆栈分配10字节的缓冲区。 If you manipulate this buffer inside a function, and then don't do anything with the pointer afterwards, you're all fine. 如果您函数内部操作此缓冲区,然后再对指针不执行任何操作,则一切都很好。 However, if you pass back the address of string -- or use the pointer in any way -- after the function returns, you're in the wrong. 但是,如果在函数返回后传递回字符串的地址-或以任何方式使用指针-您就错了。 This is because, after the function returns, you lose all of the stack allocated variables. 这是因为在函数返回后,您将丢失所有堆栈分配的变量。

There are even more ways to create strings in C (eg using malloc). 还有更多的方法可以在C中创建字符串(例如,使用malloc)。 What is your usecase? 你的用例是什么? Basically you need a place in memory where the data is stored (on the stack, on the heap, static as in your second example) and then a character pointer to the first character of your string. 基本上,您需要在内存中存储数据的位置(在第二个示例中是在堆栈上,在堆上,是静态的),然后是指向字符串第一个字符的字符指针。 Most string related functions will "see" the end of the string by the trailing '\\0', in some other cases (mostly general purpose data related functions) you also have to provide the length of the string. 大多数与字符串相关的函数将在字符串的末尾加上“ \\ 0”,在某些其他情况下(通常是与通用数据相关的函数),您还必须提供字符串的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM