我正在尝试设置以下结构的参数:
typedef struct thread_args_s {
int idx;
int role;
int fifo_desc;
char client_fifo_name[FIFO_NAME_MAX_LENGTH];
char msg[MSG_MAX_LENGTH];
} thread_args_t;
我在分配 thread_args_t->client_fifo_name
时遇到问题。
client_fifo_name 由字符串 CLIENT_FIFO_NAME_SUFFIX + i
的串联给出,使用 sprintf()
。
详细说明:
int i;
char* name;
for(i=0; i<THREAD_COUNT-1; i++){
thread_args_t* prod_args = malloc(sizeof(thread_args_t));
prod_args->idx = i;
prod_args->role = PROD_ROLE;
prod_args-> fifo_desc = server_fifo;
sprintf(name, "%s%d", CLIENT_FIFO_NAME_SUFFIX, i);
//How to assign the string "name" to prod_args->client_fifo_name?
prod_args->client_fifo_name = name;
[...]}
编译,错误为:
Incompatible types when assigning to type
char[32]
from typechar*
.
我该如何解决?
最佳答案
类型不兼容,因为赋值的左侧正好是 32 个字符,而赋值的右侧是指针。我猜,但你可能想要:
snprintf(prod_args->client_fifo_name,
FIFO_NAME_MAX_LENGTH,
"%s%d",
CLIENT_FIFO_NAME_SUFFIX, i);
关于c - 从类型 'char*' 分配给类型 char[32] 时类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794245/