C segmentation fault when printing different things

Viewed 17

I am currently making a few functions for a school project. When running each function alone and printing them, I have no errors, however when I print the result of all of the functions in main, I get a segmentation fault for the last function output.

My source code can be found Here. This is the main segment which is not working:

int main() {
    char str1[] = "382 is the best!";
    char str2[100] = {0};

    strcpy(str1, str2);
    puts(str1);
    puts(str2);

    int vec_a[3] = {12,34,10};
    int vec_b[3] = {10,20,30};
    int dot = dot_prod((char*) vec_a, (char*) vec_b, 3, sizeof(int));
    printf("%d\n", dot);
    int arr[3] = {0x12BFDA09, 0x9089CDBA, 0x56788910};
    sort_nib(arr, 3);
    for (int i = 0; i < 3; i++) {
        printf("0x%08x ", arr[i]);
    }

    printf("\n");
    return 0;
}

When running this code segment, I get the correct output for all except the final print loop, which I get a segmentation fault. However, if I comment out the puts statements and the printf(dot), the final print loop works.

Any help is greatly appreciated

0 Answers
Related