gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:43:59 25 4
gpt4 key购买 nike

我遇到了这个错误,它要求我传递 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com