1

Basically I'm trying to print something like "1 2 3"

However, when I run this on a terminal, it gives me segmentation fault without any explanation...

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

int main() {
    int width = 4;
    char complete_row[width * 3 * 2];
    char one[3], two[3], three[3], space[1];
            strcpy(space, " ");
            strcpy(complete_row, "");
            int count = 0;
            //make sure single line of color is replicated as much as width
            while (count < width) {
                strcpy(one, "1");
                strcat(one, space);
                strcpy(two, "2");
                strcat(two, space);
                strcpy(three, "3");
                strcat(complete_row, one);
                strcat(complete_row, two);
                strcat(complete_row, three);
            }
            //print that twice in the output file
            printf("%s", complete_row);
            printf("%s", complete_row);
    return 0; 
}
4
  • 5
    You don't have enough space to store strcpy(space, " "); you need room for the trailing \0: switch to space[2] Commented Sep 30, 2019 at 12:02
  • 2
    Your code is pretty convoluted, what are you actually trying to achieve? And your while loop never stops. Commented Sep 30, 2019 at 12:22
  • Did you try stepping through the code in the debugger to see which line gives you the fault? It would be helpful to point out the erring line in your post. Commented Sep 30, 2019 at 12:36
  • thanks for the help guys I finially fixed it :) Commented Sep 30, 2019 at 20:09

2 Answers 2

3

There are at least two problems in your code:

  1. the while loop never stops because you don't increment count in the loop. Therefore you concatenate a string to complete_row over and over resulting eventually in a buffer overflow which results in undefined behaviour (can be a segfault)
  2. char space[1] declares an array which can hold exactly one char, but for " " you need two chars because of the NUL string terminator. Accessing an array out of bounds results in undefined behaviour (can be a segfault).

Corrected code, see comments (untested, there may be more problems)

int main() {
    int width = 4;
    char complete_row[width * 3 * 2];
    char one[3], two[3], three[3], space[2];  // space[2]
            strcpy(space, " ");
            strcpy(complete_row, "");
            int count = 0;
            //make sure single line of color is replicated as much as width
            while (count < width) {
                strcpy(one, "1");
                strcat(one, space);
                strcpy(two, "2");
                strcat(two, space);
                strcpy(three, "3");
                strcat(complete_row, one);
                strcat(complete_row, two);
                strcat(complete_row, three);
                count++;    // increment count
            }
            //print that twice in the output file
            printf("%s", complete_row);
            printf("%s", complete_row);
    return 0; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

There are two issues in your code
1. Because of your count variable. You are not incrementing the count variable so count is 0 always and while loop is never ending or it is going infinite.
2. Size of space array is not enough.

Also for your expected result you don't have to apply the loop here as you written the code for concatenation. So removing the loop will work. Pasting the working code of yours.

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

int main() {
int width = 4;
char complete_row[width * 3 * 2*2];
char one[3], two[3], three[3], space[1];
        strcpy(space, " ");
        strcpy(complete_row, "");
        int count = 0;
        //make sure single line of color is replicated as much as width

            strcpy(one, "1");
            strcat(one, space);
            strcpy(two, "2");
            strcat(two, space);
            strcpy(three, "3");
            strcat(complete_row, one);
            strcat(complete_row, two);
            strcat(complete_row, three);

        //print that twice in the output file
        printf("%s", complete_row);

return 0;

}

3 Comments

No, strcpy(space, " "); accesses space out of bounds. An endless while loop isn't a reason to cause a segmentation fault.
You can try running my code with same strcpy(space, " ");
@KamilCuk yes it can be if strcat is called an infinite number of times, which is the case here. There are two problems in this code, the one with the infinite loop, and the one with the space array being too short.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.