理解Newt Widget Kit中指针的Const指针

时间:2016-08-27 15:06:07

标签: c pointers const

我收到此错误,要求我传递 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()的新数组,并且它实际上在其中写入用户输入的数据。这是非常误导的,指针引用的内容不会保持不变!显然,编译时我会收到警告。编译此代码的正确方法是什么?我不想违反' const'规则,我想正确编译。

对于那里的参考,我将把我的代码和属于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;
};

1 个答案:

答案 0 :(得分:1)

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]对应的内存段的前提下分配)。