0

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
GoodSpeed
  • 37
  • 4

4 Answers4

5

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.
macfij
  • 3,093
  • 1
  • 19
  • 24
3

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.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
0

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); 
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
  • The OP modifies `ipch` in their program. Your alternative will not allows modification (but `char s[2] = "?";` would). – Pascal Cuoq Aug 07 '14 at 09:33
  • Of course, but he didn't say he was going to append chars one by one. – Luis Colorado Aug 07 '14 at 09:36
  • You could say that the OP said they were to append the characters one by one by posting a program that almost does append the characters one by one, modulo the bug under discussion. – Pascal Cuoq Aug 07 '14 at 09:39
0

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.

Sathish
  • 3,740
  • 1
  • 17
  • 28
  • It could result in a Segmentation Fault, but it does not have to, and it doesn't in this particular case because with an ordinary compiler, `ipch` is placed on the stack, which is a long stretch of memory the program is allowed to access (and that almost certainly contains a null character). – Pascal Cuoq Aug 07 '14 at 09:41