简体   繁体   English

将字符串分配给char *变量

[英]Assigning a string to a char* variable

A code I am reviewing uses following string assignment 我正在审查的代码使用以下字符串分配

char *str;
str ="";

The coder then uses this 'str' to temporarily hold a string like. 然后,编码器使用此“ str”临时保留一个类似的字符串。

str = "This is a message";
fwrite(str, 1 ,strlen(str), fp);

Then this str is used again at some other place to assign a new string with a similar use. 然后,在其他地方再次使用此str来分配具有相似用途的新字符串。

I know that this works, I want to find out how exactly does this work. 我知道这有效,我想找出它到底是如何工作的。 How can you declare a char pointer and make it point to a string like that? 如何声明一个char指针并使其指向这样的字符串? What could be the maximum string length such a pointer can hold? 这样的指针可以容纳的最大字符串长度是多少? Where is this string stored? 该字符串存储在哪里? Is it automatically malloc 'd? 它是自动malloc吗?

A pointer doesn't "hold" a string, it just points to where the original string is located. 指针不会“保留”字符串,而只是指向原始字符串所在的位置。 In this case the string literal is kept as part of the program and the pointer is set to it; 在这种情况下,字符串文字将保留在程序中,并将指针设置为该字符串。 when you reassign the pointer, you're not making any copies, just setting the pointer to a different address. 当您重新分配指针时,您并没有进行任何复制,只是将指针设置为其他地址。

The maximum size of the string is thus the maximum size of a string literal, which will depend on the compiler and the amount of available program space. 因此,字符串的最大大小是字符串文字的最大大小,这取决于编译器和可用程序空间的数量。

If you want to actually make a copy of a string, first you must allocate some storage for it which must be one greater than the number of characters. 如果要实际复制一个字符串,则首先必须为其分配一些存储空间,该存储空间必须比字符数大一个。 Then use strcpy to make the copy. 然后使用strcpy进行复制。

This string is statically contained in the object module. 此字符串静态包含在对象模块中。 You don't need to malloc memory for such strings, because they already have a memory assigned by the compiler. 您不需要为此类字符串分配内存,因为它们已经由编译器分配了内存。 Because of this, you also can not free such a pointer. 因此,您也不能释放这样的指针。 If you look with an hex editor in your exe file, you can see that such a string is contained inside it, as opposed to a dynamically allocated string, which only exists in memory as long as the executable runs. 如果在exe文件中使用十六进制编辑器,则可以看到其中包含这样的字符串,而不是动态分配的字符串,后者仅在可执行文件运行时才存在于内存中。

The maximum size of such a string depends on your compiler. 这样的字符串的最大大小取决于您的编译器。

char* is just a pointer to a char (or series of them). char*只是指向char(或其中的一系列)的指针。

You can have it pointing to any "string" you like. 您可以使其指向您喜欢的任何“字符串”。 In the examples given, they are just changing the pointer's value (ie what str points to). 在给出的示例中,它们只是在更改指针的值(即str指向的值)。

char *name;

name="some string"; //the name points to the address of location of the string. // name指向字符串位置的地址。

is not similar to : 与以下内容不相似:

char str[]; char str [];

str="some string";// remember this type of statement won't work,because str is about to store characters but you are assigning the pointer. str =“ some string”; //请记住,这种类型的语句将不起作用,因为str将要存储字符,但是您正在分配指针。

A constant character string always represents a pointer to that string. 常量字符串始终表示指向该字符串的指针。

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

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