简体   繁体   中英

concat char* with char netbea

I need to concatenate a string with a char in c but I not figured it out. This is part of my code:

  unsigned char c ='d';
  char *respuesta;

   while(ciclo)
    {
        nanosleep((struct timespec[]){{0, INTERVAL_MS}}, NULL);
        //veces++;

       if (read(tty_fd,&c,1)>0)
       {
         write(STDOUT_FILENO,&c,1);
         respuesta = append(respuesta,c); 
       }
       else{ciclo = false;}
    }

    void append(char* s, char *c)
    {
        int len = strlen(s);
        s[len] = c; // in this line I got the error.
        s[len+1] = '\0';
    }

thanks in advance.

Change:

void append(char* s, char *c)

to:

void append(char* s, char c)

since you want to append a char (single character) to a char * (string).

Also make sure that you have allocated (sufficient) memory to respuesta - it's just a wild pointer in the code above, but hopefully you have actually malloc da suitable amount of storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM