预期‘;const char*uuu restrict_uuu’;但参数的类型为‘;int’;

预期‘;const char*uuu restrict_uuu’;但参数的类型为‘;int’;,c,C,我看到了一些关于它的线索,但仍然不知道如何修复这个错误。问题是: 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转换);

我看到了一些关于它的线索,但仍然不知道如何修复这个错误。问题是:

    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
但是

奇怪,因为

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

snprintf的第一个参数是什么? 你要通过什么

它们是一样的吗

如果编译器允许您调用该函数,您认为snprintf函数究竟会做什么

情况也一样糟糕。第一个参数是未初始化的指针,因此它将崩溃。第二个参数是char而不是指针,因此它将崩溃。不知道SHM_的尺寸是多少,但到目前为止,我担心情况会更糟


这看起来真的像是你在不知道他们在做什么的情况下,从不同的来源一起复制零碎的东西

这可以通过以下方法完成

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

请阅读手册

1-第一个参数是char变量,我认为它将被复制到do DATA变量。我想使用getc函数将特定字符从文件传递到数据变量。2-不明白?3-我有一个错误,对不起,已经修复了。我想存储数据,聊天变量谢谢!我还有一个问题。。。。strncpy(数据“a”,SHM_尺寸)//完全可以工作,但是char chat[SHM_SIZE];snprintf(chat,sizeof chat,“%d”,getc(文件));printf(“变量%c\n”,chat)//因为printf(“字符:%c”,getc(文件)),所以没有显示任何东西或一些奇怪的符号//显示所有内容OK因为
%c
是错误的说明符,我修复了答案。我建议您启用编译器警告,这将为您节省很多问题。
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