-4

I am reading off a fifo, and then I try to make a another array in different format out of it. I know buf gets its values as expected, since printing it out is good.

char graphic[100];
char buf[100];
read(fd_read,&buf,50);
for(int i=0; i<10; i++){
strcat(graphic, buf[i]); }

gives me error:

passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion]

I use eclipse on linux, on C. ...HELP?

aviadm71
  • 53
  • 1
  • 9

2 Answers2

4

Signature of strcat is:

char *strcat(char *dest, const char *src);

i.e. second argument should be pointer to char array that terminates with '\0'. In your case you are passing a single character.

Instead of for(int i=0; i<10; i++){ strcat(graphic, buf[i]); }, you possibly need just: (Assuming graphic is initialized somewhere)

strcat(graphic, buf); /* Use strncat for limited number of characters */

Also make sure that string.h is included in your program.


To append the characters by yourself, you can use just assignation than using a function.

/* Append one character (ith character of buf[i] ) */
const size_t len = strlen(graphic);
if(len + 1 >= sizeof graphics) { /* Error handling */ }
else {
  graphic[len] = buf[i];
  graphic[len + 1] = '\0';
}
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
0

syntaxt of strcat():

char *strcat(char *dest, const char *src);

The two parameters should be character pointers. In your code, buf is a character pointer(or an array) but in the second parameter to the call to strcat, you didn't send the entire array(buf) thus making it as a character.

try this instead, this might meet your requirements:

char graphic[100];
char buf[100];
read(fd_read, &buf, 50);

strcpy(graphic, "");
strncat(graphic, buf, 10); //last parameter tells you how many bytes to append

Once refer the below link for more information regarding the error:

C strcat - warning: passing arg 2 of `strcat' makes pointer from integer without a cast

Community
  • 1
  • 1
  • `strcat()`ing to an uninitialised buffer (`graphic` here) invokes the infamous Undefined Behaviour. – alk Apr 20 '16 at 12:18
  • @alk, yes. Garbage values will be present in the graphics array to which the buffer array is appended. I have edited my answer in order to remove such kind of behaviour. :-) –  Apr 20 '16 at 16:01