使用结构中的 ncurses 扩展字符

Using ncurses extended character from struct

我正在尝试显示从结构中提取的 ncurses 扩展字符。

当我尝试使用时:

#include <ncurses.h>
struct TEST_STRUCT
{
    char    nCharacter;     // Where I want to store variable for printed character
    short   nTestNumber;    // Other stuff in struct
};
TEST_STRUCT sTestData[] = {
    { '.', 1 },         // Period
    { ',', 2 },         // Comma
    { ACS_VLINE, 1 }    // Vertical Line
};
int main(void)
{
    initscr();
    clear();
    for( int n = 0; n < 3; n++)
    {
        addch(sTestData[n].nCharacter); // print the characters in the struct
    }
        refresh();
    endwin();
    return 0;
}

ACS_VLINE字符显示不正常,经过一番折腾,发现了以下作品:

#include <ncurses.h>
struct TEST_STRUCT
{
    int     nCharacter;         // Where I want to store variable for printed character
    short   nTestNumber;    // Other stuff in struct
};
TEST_STRUCT sTestData[] = {
    { '.', 1 },     // Period
    { ',', 2 },     // Comma
    { 4194424, 1 }  // Vertical Line
};
int main(void)
{
    initscr();
    clear();
    for( int n = 0; n < 3; n++)
    {
        addch(sTestData[n].nCharacter);     // print the characters in the struct
    }
    endwin();
    return 0;
}

将数值存储在 int 中似乎是错误的,但它确实有效。我应该怎么做才能做到这一点 "correctly".

你的第一个例子的问题是符号 ACS_VLINE 是数组中的一个条目,它不是静态初始化的(它的实际内容取决于 initscr)。奇怪的是,g++ 不会对此发出警告,但 gcc -Wall 会发出警告。

定义如下:

#define NCURSES_ACS(c)  (acs_map[NCURSES_CAST(unsigned char,c)])

#define ACS_VLINE       NCURSES_ACS('x') /* vertical line */

第二种情况下的常数不同,等于 A_ALTCHARSET 结合 x:

#define NCURSES_ATTR_SHIFT       8
#define NCURSES_BITS(mask,shift) ((mask) << ((shift) + NCURSES_ATTR_SHIFT))

#define A_ALTCHARSET    NCURSES_BITS(1UL,14)

正如 changelog from 2003 中所暗示的那样,自 2000 年代初以来,这就是一个区别。