简体   繁体   English

C strcat()给出错误的附加字符串

[英]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 . 这将强制仅从ipch追加一个字节。 strcat() will continue appending some bytes, since there's no null termination character after the char you are appending. strcat()将继续追加一些字节,因为要追加的char后面没有空终止符。 as others said, strcat might find somewhere in memory \\0 and then terminate, but if not, it can result in segfault. 正如其他人所说,strcat可能会在\\0内存中找到某个位置,然后终止,但如果不这样做,则可能导致segfault。

from manpage: 从联机帮助页:

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

The strncat() function is similar to strcat(), except that strncat()函数类似于strcat(),除了

  • it will use at most n characters from src; 它将最多使用src中的n个字符; and
  • src does not need to be null-terminated if it contains n or more characters. 如果src包含n个或更多字符,则无需以null结尾。

strcat requires its second argument to be a pointer to a well-formed string. strcat要求其第二个参数是指向格式正确的字符串的指针。 &ipch does not point to a well-formed string (the character sequence of one it points to lacks a terminal null character). &ipch不会指向格式正确的字符串(它指向的字符串的字符序列缺少末尾的空字符)。

You could use char ipch[2]=" "; 您可以使用char ipch[2]=" "; to declare ipch . 宣布ipch In this case also use: 在这种情况下,请使用:

  • strcat(ipstr,ipch); to append the character to ipstr . 将字符附加到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. 当您在原始程序&ipch传递给strcat ,发生的情况是该函数strcat假定字符串继续,并读取内存中的下一个字节。 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读取一些垃圾字符,然后意外找到空字符的情况。

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. strcat()用于连接字符串...因此仅传递一个char指针是不够的...您必须在该字符后接一个'\\ 0'char,然后传递该对象的指针。 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. strcat函数用于连接两个字符串。 Not a string and a character. 不是字符串和字符。 Syntax- 句法-

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

so you need to pass two strings to strcat function. 因此您需要将两个字符串传递给strcat函数。

In your program 在你的程序中

strcat(ipstr,&ipch);

it is not a valid statement. 这不是有效的声明。 The second argument ipch is a char . 第二个参数ipchchar you should not do that. 你不应该那样做。 It results in Segmentation Fault . 这会导致Segmentation Fault

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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