简体   繁体   中英

C strcat() gives wrong appended string

I am appending a string using single character, but I am not able to get it right. I am not sure where I am making mistake. Thank you for your help in advance. The original application of the method is in getting dynamic input from user.

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

void main(){
    int j;
    char ipch=' ';
    char intext[30]="What is the problem";
    char ipstr[30]="";
    printf("Input char: ");
    j=0;
    while(ipch!='\0'){
        //ipch = getchar();
        ipch = intext[j];
        printf("%c", ipch);
        strcat(ipstr,&ipch);
        j++;
    }
    puts("\n");
    puts(ipstr);
    return;
  }

Following is the output I am getting.

$ ./a.out 
Input char: What is the problem

What is h  e

 p
oblem

change

strcat(ipstr,&ipch);

to

strncat(ipstr, &ipch, 1);

this will force appending only one byte from ipch . strcat() will continue appending some bytes, since there's no null termination character after the char you are appending. as others said, strcat might find somewhere in memory \\0 and then terminate, but if not, it can result in segfault.

from manpage:

char *strncat(char *dest, const char *src, size_t n);

The strncat() function is similar to strcat(), except that

  • it will use at most n characters from src; and
  • src does not need to be null-terminated if it contains n or more characters.

strcat requires its second argument to be a pointer to a well-formed string. &ipch does not point to a well-formed string (the character sequence of one it points to lacks a terminal null character).

You could use char ipch[2]=" "; to declare ipch . In this case also use:

  • strcat(ipstr,ipch); to append the character to ipstr .

  • ipch[0] = intext[j]; to change the character to append.


What happens when you pass &ipch to strcat in your original program is that the function strcat assumes that the string continues, and reads the next bytes in memory. A segmentation fault can result, but it can also happen that strcat reads a few garbage characters and then accidentally finds a null character.

strcat() is to concatenate strings... so passing just a char pointer is not enough... you have to put that character followed by a '\\0' char, and then pass the pointer of that thing. As in

/* you must have enough space in string to concatenate things */
char string[100] = "What is the problem";
char *s = "?"; /* a proper '\0' terminated string */
strcat(string, s);
printf("%s\n", string); 

strcat function is used to concatenate two strings. Not a string and a character. Syntax-

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

so you need to pass two strings to strcat function.

In your program

strcat(ipstr,&ipch);

it is not a valid statement. The second argument ipch is a char . you should not do that. It results in Segmentation Fault .

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