c - 了解 Newt Widget Kit 中指向指针的 Const 指针

标签 c pointers constants

我遇到了这个错误,它要求我传递 const char** 而不仅仅是 char**:

client.c:34:33: warning: passing argument 5 of ‘newtEntry’ from incompatible pointer type [-Wincompatible-pointer-types]
  fld_email=newtEntry(16,1,"",20,&email_buf,NEWT_FLAG_SCROLL);
                                 ^
In file included from client.c:4:0:
/usr/include/newt.h:249:15: note: expected ‘const char **’ but argument is of type ‘char **’
 newtComponent newtEntry(int left, int top, const char * initialValue, int width,

调用函数定义如下:

newtComponent newtEntry(int left, int top, const char * initialValue, int width,
            const char ** resultPtr, int flags) {
....
}

我的问题是,为什么 newtEntry() 函数需要指向 char 的指针,如果在其中,它使用 malloc() 分配新数组,并且实际上将用户输入的数据写入其中。这是非常误导的,指针引用的内容不会保持不变!显然,在编译时我收到了警告。编译此代码的正确方法是什么?我不想违反“常量”规则,我想正确编译。

为了便于引用,我将放置我的代码,以及属于 Newt 库的函数本身的代码。

我使用 Newt 库的代码:

err_code_t read_account_data(void) {
    #define WINDOW_WIDTH    40
    #define WINDOW_HEIGHT   10
    char *email_buf,*password_buf;
    int width,height;
    int ret;
    newtComponent form,fld_email,fld_password,lbl_email,lbl_password;

    newtGetScreenSize(&width,&height);
    if ((width<WINDOW_WIDTH) || (height<WINDOW_HEIGHT)) {
        return(ERR_SCREEN_SIZE_TOO_SMALL);
    }
    ret=newtCenteredWindow(WINDOW_WIDTH,WINDOW_HEIGHT,"Account data");
    if (ret) {
        return ERR_SYS_ERROR;
    }
    form=newtForm(NULL,NULL,0);
    lbl_email=newtLabel(1,1,"Email:");
    fld_email=newtEntry(16,1,"",20,&email_buf,NEWT_FLAG_SCROLL);
    lbl_password=newtLabel(1,3,"Password:");
    fld_password=newtEntry(16,3,"",20,&password_buf,NEWT_FLAG_SCROLL|NEWT_FLAG_RETURNEXIT|NEWT_FLAG_PASSWORD);
    newtFormAddComponents(form,lbl_email,fld_email,lbl_password,fld_password,NULL);
    newtRunForm(form);

    newtPopWindow();
    return ERR_NO_ERROR;
    #undef WINDOW_WIDTH
    #undef WINDOW_HEIGHT
}

Fedora 存储库中 Newt 库源代码中 newtEntry() 函数的代码:

newtComponent newtEntry(int left, int top, const char * initialValue, int width,
            const char ** resultPtr, int flags) {
    newtComponent co;
    struct entry * en;

    co = malloc(sizeof(*co));
    en = malloc(sizeof(struct entry));
    co->data = en;

    co->top = top;
    co->left = left;
    co->height = 1;
    co->width = width;
    co->isMapped = 0;
    co->callback = NULL;
    co->destroyCallback = NULL;

    co->ops = &entryOps;

    en->flags = flags;
    en->cursorPosition = 0;
    en->firstChar = 0;
    en->bufUsed = 0;
    en->bufAlloced = width + 1;
    en->filter = NULL;

    if (!(en->flags & NEWT_FLAG_DISABLED))
    co->takesFocus = 1;
    else
    co->takesFocus = 0;

    if (initialValue && strlen(initialValue) > (unsigned int)width) {
    en->bufAlloced = strlen(initialValue) + 1;
    }
    en->buf = malloc(en->bufAlloced);
    en->resultPtr = resultPtr;
    if (en->resultPtr) *en->resultPtr = en->buf;

    memset(en->buf, 0, en->bufAlloced);
    if (initialValue) {
    strcpy(en->buf, initialValue);
    en->bufUsed = strlen(initialValue);
    en->cursorPosition = en->bufUsed;

    /* move cursor back if entry is full */
    if (en->cursorPosition && !(en->flags & NEWT_FLAG_SCROLL ||
            wstrlen(en->buf, -1) < co->width))
        en->cursorPosition = previous_char(en->buf, en->cursorPosition);
    } else {
    *en->buf = '\0';
    en->bufUsed = 0;
    en->cursorPosition = 0;
    }

    en->cs = NEWT_COLORSET_ENTRY;
    en->csDisabled = NEWT_COLORSET_DISENTRY;

    return co;
}

结构体入口:

struct entry {
    int flags;
    char * buf;
    const char ** resultPtr;
    int bufAlloced;
    int bufUsed;        /* amount of the buffer that's been used */
    int cursorPosition;     /* cursor *in the string* on on screen */
    int firstChar;  /* first character position being shown */
    newtEntryFilter filter;
    void * filterData;
    int cs;
    int csDisabled;
};

最佳答案

const char** resultPtr 含义如下:

  • 不能**resultPtr赋值。
  • 可以char*类型的值分配给*resultPtr
  • 可以char** 类型的值分配给resultPtr

这意味着:

  • malloc 可以将内存地址写入*resultPtr
  • 不能修改(*resultPtr)[i] 中任何索引i 的字符。

如果您希望修改存储在 (*resultPtr)[i] 中的索引 i 的值,您可以执行以下操作:

char** anotherPtr;
anotherPtr = (char**)resultPtr;

然后你可以修改(*anotherPtr)[i]中的字符为任何索引i(前提是内存段对应于变量(*anotherPtr)[i] 被分配。

关于c - 了解 Newt Widget Kit 中指向指针的 Const 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39182561/

相关文章:

在 C 中不使用 strcmp 比较 Char 数组

c++ - 在 C++ 中声明一个 const int 数组

当我不知道它是否是临时的时,C++ 返回类型

java - C 指针 - 从 Char 到 Char 的无效转换*

c++ - 人脸识别分类器

c - 给定 C 中的字符串链表,递归地找到 n 个单词排列?

c++ - c++中的 vector 是指针吗?

c - 无法使用 mmap 从结构中读取指针

C# 使用常量加速解析器?虽然是抽象类

c - 当我们将返回类型为 void 的函数调用返回值分配给 int 变量时会发生什么?