S
S
skilet162021-11-22 23:39:26
C++ / C#
skilet16, 2021-11-22 23:39:26

Why does strcat() stop the program?

C program stops execution at strcat() function

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

int main(void)
{
    int x = -1, z = 1;
    char string[10] = { '\0' };


    while (x != 0)
    {
        printf("Vvedit chislo = ");
        scanf_s("%d", &x);
        z = x * z;
        strcat(string, z);
    }

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

    
    _getch();
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Demin, 2021-11-22
@includedlibrary

Because you need to pass a pointer to a string to strcat, you pass the variable z as an int. As a result, z is interpreted as the address at which the string lies and a segmentation error occurs.

C
CityCat4, 2021-11-23
@CityCat4

The prototype of the strcat() function: does it suggest anything? :)
char *strcat(char *dest, const char *src);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question