gpt4 book ai didi

c - 将 char 缓冲区分配给指针数组

转载 作者:太空狗 更新时间:2023-10-29 15:53:36 25 4
gpt4 key购买 nike

海湾合作委员会 4.4.4 c89

warning assignment makes integer from pointer without a cast
**devices = device_buff;
warning: value computed is not used
*devices++;

我通过以下代码收到上述警告。我想要做的是从用户那里获得输入。并将该 char 数组分配给一个指针数组。所以我的指针数组将包含所有输入的设备。但是,我在这条线上收到了一个 UB:

**devices = device_buff;

非常感谢任何建议,

static void device_input()
{
#define DEVICE_SIZE 80
char device_buff[DEVICE_SIZE] = {0};
char **devices = NULL;
size_t i = 0;

for(i = 0; i < 3; i++) {
printf("Enter device name: ");
fgets(device_buff, (size_t)DEVICE_SIZE, stdin);

**devices = device_buff;
*devices++;
}

/* NULL terminate last element */
*devices = NULL;

printf("Display devices\n");
while(*devices != NULL) {
printf("Device [ %s ]\n", *devices++);
}
}

最佳答案

**devices是一个char,device_buff是一个char数组。这两种类型是不相容的。

关于c - 将 char 缓冲区分配给指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3435197/

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