【问题标题】:Understanding Const Pointer to Pointer in Newt Widget Kit了解 Newt Widget Kit 中的 Const Pointer to Pointer
【发布时间】:2016-08-27 15:06:07
【问题描述】:

我收到此错误,这需要我传递 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
}

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

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;
};

【问题讨论】:

  • @alk 是的,它确实可以编译。您可以在这个网址下载完整的源代码:fedorahosted.org/newt,我已经添加了 struct 的定义,它在 Newt 源的 entry.c 文件开头声明

标签: c pointers constants


【解决方案1】:

const char** resultPtr 表示如下:

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

这意味着:

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

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

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

那么你可以为任意索引i修改(*anotherPtr)[i]中的字符(前提是分配了变量(*anotherPtr)[i]对应的内存段)。

【讨论】:

  • “anotherPtr”的最后一部分不起作用。在此代码 "email=email_buf; email[3]='*'; printf("modified email: %s\n",email);" 之后出现此错误:警告:赋值从指针目标类型中丢弃“const”限定符
  • 如果我理解正确,Newt 小部件套件不允许修改用户输入的值。我能做到的唯一方法是将数据复制到另一个内存位置,对吗?
  • resultPtrnewtEntry函数中被声明为const的原因可能是返回值co(co-&gt;data-&gt;resultPtr = resultPtr)中也引用了resultPtr。所以如果你覆盖**resultPtr,它会对co产生副作用。而且这样的副作用可能是不受欢迎的,因此将resultPtr 声明为const 会更安全。一种选择是将数据复制到另一个char-array。另一种选择是简单地忽略警告——警告不是错误!也许您甚至可以使用 #pragma 预编译器选项停用警告。
猜你喜欢
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多