Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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
用于2字节wchar\u t的swprintf_C_Linux_Unix - Fatal编程技术网

用于2字节wchar\u t的swprintf

用于2字节wchar\u t的swprintf,c,linux,unix,C,Linux,Unix,如何使swprintf接受2字节的wchar\t 我试过这个: #include <wchar.h> #undef wchar_t #define wchar_t uint16_t path_length = (strlen(host) + strlen(share) + 4); wchar_t *path = (uint16_t*)malloc(path_length*sizeof(uint16_t)); swprintf(path, path_length, L"\\\\%s\

如何使swprintf接受2字节的wchar\t

我试过这个:

#include <wchar.h>
#undef wchar_t
#define wchar_t uint16_t

path_length = (strlen(host) + strlen(share) + 4);
wchar_t *path = (uint16_t*)malloc(path_length*sizeof(uint16_t));
swprintf(path, path_length, L"\\\\%s\\%s", host, share);
我想这与restrict关键字有关。 有没有更简单的方法?我需要一个2字节的wchar\t,因为我正在尝试生成一个16位unicode字符串

我知道编译器选项“-fshort wchar”,但我不想走这条路


我还希望避免手动将字节复制到另一个数组中,因为这样我就不得不担心endianness,这也会增加时间和空间的复杂性。

在MSVC中,
sizeof(wchar\u t)==2
但是
sizeof(uint16\u t)
是未知类型。我不明白为什么你甚至不使用
wchar\u t
就使用
#define wchar\u t uint16\u t
。正如编译器错误告诉您的那样,您正在将
uint16\u t
传递给
swprintf()
,而它需要
wchar\u t
wchar\u t*path=malloc(path\u length*sizeof(wchar\u t))有什么问题?(注意:请不要在C中使用
malloc()
)。对不起,这实际上是一个输入错误。我将使用unicode.org中的unicode转换代码。
test.c:104:5: error: passing argument 1 of ‘swprintf’ from incompatible pointer type [-Werror]
In file included from test.c:13:0:
/usr/include/wchar.h:603:12: note: expected ‘wchar_t * restrict’ but argument is of type ‘uint16_t *’