C语言 哪种方法适合初始化 wchar_t 字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15951574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which method is correct for Initializing a wchar_t string?
提问by Neon Flash
I am writing a program and I need to initialize a message buffer which will hold text. I am able to make it work, however I am writing below various ways used to initialize the strings in C and I want to understand the difference. Also, which is the most appropriate method for initializing a wchar_t/char string?
我正在编写一个程序,我需要初始化一个将保存文本的消息缓冲区。我能够使它工作,但是我在下面写了用于在 C 中初始化字符串的各种方法,我想了解它们之间的区别。另外,初始化 wchar_t/char 字符串最合适的方法是什么?
Method I:
方法一:
wchar_t message[100];
based on my understanding, this will allocate a memory space of 200 bytes (I think size of wchar_t is 2 bytes on Windows OS). This memory allocation is static and it will be allocated inside the .data section of the executable at the time of compiling.
根据我的理解,这将分配 200 字节的内存空间(我认为 wchar_t 的大小在 Windows 操作系统上是 2 字节)。此内存分配是静态的,将在编译时在可执行文件的 .data 部分内分配。
message is also a memory address itself that points to the first character of the string.
message 也是一个内存地址本身,指向字符串的第一个字符。
This method of initializing a string works good for me.
这种初始化字符串的方法对我很有用。
Method II:
方法二:
wchar_t *message;
message=(wchar_t *) malloc(sizeof(wchar_t) * 100);
This method will first initialize the variable message as a pointer to wchar_t. It is an array of wide characters.
该方法首先将变量消息初始化为指向 wchar_t 的指针。它是一个宽字符数组。
next, it will dynamically allocate memory for this string. I think I have written the syntax for it correctly.
接下来,它将为此字符串动态分配内存。我想我已经正确地为它编写了语法。
When I use this method in my program, it does not read the text after the space in a string.
当我在我的程序中使用此方法时,它不会读取字符串中空格后的文本。
Example text: "This is a message"
It will read only "This" into the variable message and no text after that.
它只会将“This”读入变量消息,之后不会再有文本。
Method III:
方法三:
wchar_t *message[100];
This will define message as an array of 100 wide characters and a pointer to wchar_t. This method of initializing message works good. However, I am not sure if it is the right way. Because message in itself is pointing to the first character in the string. So, initializing it with the size, is it correct?
这将把消息定义为一个包含 100 个宽字符的数组和一个指向 wchar_t 的指针。这种初始化消息的方法效果很好。但是,我不确定这是否是正确的方法。因为消息本身指向字符串中的第一个字符。那么,用大小初始化它,是否正确?
I wanted to understand it in more depth, the correct way of initializing a string. This same concept can be extended to a string of characters as well.
我想更深入地理解它,初始化字符串的正确方法。同样的概念也可以扩展到字符串。
采纳答案by Femaref
It really depends on what you want to do and how you use the data. If you need it globally, by all means, define a static array. If you only need it in a method, do the same in the method. If you want to pass the data around between functions, over a longer lifetime, mallocthe memory and use that.
这实际上取决于您想做什么以及如何使用数据。如果您需要全局,请务必定义一个静态数组。如果你只需要在一个方法中使用它,在方法中做同样的事情。如果你想在函数之间传递数据,在更长的生命周期内,malloc内存和使用它。
However, your method III is wrong - it is an array of 100 wchar_tpointers. If you want to create a 100 large wchar_tarray and a pointer, you need to use:
但是,您的方法 III 是错误的 - 它是一个包含 100 个wchar_t指针的数组。如果要创建一个 100 的大wchar_t数组和一个指针,则需要使用:
wchar_t message[100], *message_pointer;
Also, concerning terminology: you are only declaringa variable in the method I, you never assign anything to it.
另外,关于术语:您只是在方法 I 中声明了一个变量,您永远不会为其分配任何内容。
回答by alk
The magic is the encoding-prefixL:
神奇的是encoding-prefixL:
#include <wchar.h>
...
wchar_t m1[] = L"Hello World";
wchar_t m2[42] = L"Hello World";
wchar_t * pm = L"Hello World";
...
wcscat(m2, L" again");
pm = calloc(123, sizeof *pm);
wcspy(pm, L"bye");
See also the related part of the C11 Standard.

