简体   繁体   English

无法将宽字符分配到宽字符字段。

[英]Can't assign wide char into wide char field.

I have been given this school project. 我得到了这个学校项目。 I have to alphabetically sort list of items by Czech rules. 我必须按捷克规则按字母顺序对项目清单进行排序。 Before I dig deeper, I have decided to test it on a 16 by 16 matrix so I did this: 在深入研究之前,我决定在16 x 16矩阵上进行测试,所以我这样做:

typedef struct {    
wint_t **field;
}LIST;

...

setlocale(LC_CTYPE,NULL);

....

list->field=(wint_t **)malloc(16*sizeof(wint_t *));
for(int i=0;i<16;i++)  
list->field[i]=(wint_t *)malloc(16*sizeof(wint_t));

In another function I am trying to assign a char. 在另一个函数中,我试图分配一个字符。 Like this: 像这样:

sorted->field[15][15] = L'C';
wprintf(L"%c\n",sorted->field[15][15]);

Everything is fine. 一切顺利。 Char is printed. 字符被打印。 But when I try to change it to 但是当我尝试将其更改为

sorted->field[15][15] = L'Č';

It says: Extraneous characters in wide character constant ignored. 它说:宽字符常量中的无关字符被忽略。 (Xcode) And the printing part is skipped. (Xcode)并且打印部分被跳过。 The main.c file is in UTF-8. main.c文件位于UTF-8中。 If I try to print this: 如果我尝试打印此:

printf("ěščřžýááíé\n");

It prints it out as written. 它按书面形式打印出来。 I am not sure if I should allocate mem using wint_t or wchar_t or if I am doing it right. 我不确定是否应该使用wint_t或wchar_t分配mem或我做对了。 I tested it with both but none of them works. 我对两者都进行了测试,但没有一个起作用。

clang seems to support entering arbitrary byte sequences into to wide strings with the \\x notation: clang似乎支持使用\\x表示法将任意字节序列输入到宽字符串中:

wchar_t c = L'\x2126';

This compiles without notice. 编译时不另行通知。

Edit: Adapting what I find on wikipedia about wide characters , the following works for me: 编辑:改编我在Wikipedia上找到的有关宽字符的内容 ,以下对我有用:

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>

int main(void)
{
    setlocale(LC_ALL,"");
    wchar_t myChar1 = L'\x2126';
    wchar_t myChar2 = 0x2126;  // hexadecimal encoding of char Ω using UTF-16
    wprintf(L"This is char: %lc \n",myChar1);
    wprintf(L"This is char: %lc \n",myChar2);
}

and prints nice characters in my terminal. 并在我的终端上打印漂亮的字符。 Make sure that your teminal is able to interpret utf-8 characters. 确保您的终端能够解释utf-8字符。

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

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