Expected ‘const char * __restrict__’ but argument is of type ‘int’

c

I saw few threads about it but still no clue how to fix that error. The thing is:

    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

Please help 🙂

sprintf – gives error (int to char conversion);
atoi/itoa – not working :/

Edit: @iharob
thank you! i still have a problem….

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

but

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

weird because

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

Best Answer

This could be done with the following

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

please read the snprintf manual