gpt4 book ai didi

c - 应为 ‘const char * __restrict__’,但参数的类型为 ‘int’

转载 作者:行者123 更新时间:2023-11-30 17:23:09 25 4
gpt4 key购买 nike

我看到了一些关于它的帖子,但仍然不知道如何修复该错误。事情是这样的:

    char *data;
char chat;
snprintf(chat,"%d",getc(file));//error here
printf("\Variable %c",chat); // here is still valid
strncpy(data, chat, SHM_SIZE); //error here

请帮忙:)

sprintf - 给出错误(int 到 char 转换);atoi/itoa - 不工作:/

编辑:@iharob谢谢你!我还有一个问题......

   strncpy(data, "a", SHM_SIZE); //is totally working

但是

 char chat[SHM_SIZE];  
snprintf(chat, sizeof chat, "%d",getc(file));
printf("Variable %c\n", chat);//showing nothing or some weird signs

很奇怪,因为

printf("Character : %c",getc(file)); //shows everything ok

最佳答案

这可以通过以下方式完成

char *data;
char chat[SHM_SIZE];

/* snprintf signature is snprintf(char *str, size_t size, const char *format, ...); */
/* read the manual please */
snprintf(chat, sizeof chat, "%d",g etc(file));
printf("Variable %s\n", chat); // here is still valid

length = strlen(chat);
/* you need to allocate space for the destination */
data = malloc(1 + length); /* 1+ for the terminating null byte which strlen does not count */
if (data != NULL) /* check that malloc succeeded. */
strncpy(data, chat, length); //error here

请阅读snprintf手册

关于c - 应为 ‘const char * __restrict__’,但参数的类型为 ‘int’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672571/

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