Home > database >  cannot append string in C (program stops)
cannot append string in C (program stops)

Time:05-26

I'm trying to append a string, but whenever it gets to the strcat() function the program exits without printing the string.

I've been running this code from within Visual Studio as well as from its own .exe file and get the same result. Can anybody tell me what I'm doing wrong?

main()
{
    char str[100], slash = 'H';

    gets(str);

    printf("\n\n%s\n\n%i\n\n", str, strlen(str));

    strcat(str, slash);

    printf("\n%s", str);
}

CodePudding user response:

The function gets is unsafe and is not supported by the C Standard. Instead you standard C function fgets.

The function strcat is declared the following way

char * strcat(char * restrict s1, const char * restrict s2);

That is it expects two arguments of the pointer type char * that point to string literal. However the variable slash is declared as having the type char

char str[100], slash = 'H';

Instead you should write the declaration the following way

char str[100], *slash = "H";

Now the variable slash has the pointer type char * and points to the string literal "H".

Pay attention to that you should guarantee that the array str has a space to accommodate the string literal.

Also to output the returned value of the function strlen you have to use the conversion specifier zu instead of i or d

printf("\n\n%s\n\n%zu\n\n", str, strlen(str));

Also bear in main that according to the C Standard the function main without parameters shall be declared like

int main( void )

Actually if you need to append only one character to a string then it can be done without the function strcat the following way

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

int main( void )
{
    char str[100], slash = 'H';

    fgets( str, sizeof( str ), stdin );

    size_t n = strcspn( str, "\n" );

    if ( n != sizeof( str ) - 1 )
    {
        str[n] = slash;
        str[  n] = '\0';
    }

    printf( "\n%s\n", str );
}

CodePudding user response:

char * strcat ( char * destination, const char * source ); this is a declaration of strcat(), so the second type should be a pointer to c-string, but you are passing a single character.

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

int main()
{
    char str[100], slash[] = "H";

    fgets(str, sizeof(str), stdin);
    str[strcspn(str, "\n")] = '\0';

    printf("\n\n%s\n\n%i\n\n", str, strlen(str));

    strcat(str, slash);

    printf("\n%s", str);

    return 0;
}

Just convert slash to be c-string instead of a single character.

CodePudding user response:

To append a char to a char * you can also use sprintf() with the correct format specifiers:

char str[100] = {0}, slash = 'H';//{0} initializes all elements of str to 0
char buf[100] = {0};//same
fgets(str, sizeof str, stdin);//fgets - safer to use than gets()
sprintf(buf, "%s%c", str, slash);//note format specifier for char in 2nd position
...

There are usually many ways to do what is needed in C, particularly when manipulating strings.

  • Related