Home > Software engineering >  C Segmentation Fault while converting numbers to strings
C Segmentation Fault while converting numbers to strings

Time:04-19

I have been coding for a couple years now and wanted to learn C. I need a function that does not use printf but instead ends up with a string as output since I am trying to target a platform with custom I/O. The function needs to calculate prime factors of a given int n.

I have tested the function that evaluates the prime factors before and it worked, however, the string conversion function has not worked yet. The segmentation fault error:

    ~/Development/C/First  ./run.sh                                                                                                                                           ✔ 
./run.sh: line 2: 21387 Segmentation fault      (core dumped) ./a.out
Compilation failed!

Code:

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

int* primeFactors(int n);
char* primeFactorsAsString(int n);

int main()
{
    int number = 117;
    printf(primeFactorsAsString(number));
    return 0;
}

int* primeFactors(int n)
{
    if (n < 2) {
        static int result[2];
        result[0] = 1;
        result[1] = n;
        return result;
    }

    int size = 0;
    static int factors[33];
    //Max int has 32 factors, plus first spot in array for length of array.

    while (n%2 == 0)
    {
        size  ;
        factors[size] = 2;
        n = n/2;
    }
 
    for (int i = 3; i <= sqrt(n); i = i 2)
    {
        while (n%i == 0)
        {
            size  ;
            factors[size] = i;
            n = n/i;
        }
    }
    if (n > 2)
    {
        size  ;
        factors[size] = n;
    }

    factors[0] = size;
    return factors;
}

char* primeFactorsAsString(int n) {
    int *factors;

    if (factors[0] > 25) {
        return "TO BIG";
    }

    factors = primeFactors(n);
    static char result[100];
    char buffer[100];
    for (int i = 1; i < result[0]; i  )
    {
        sprintf(buffer, "%d * ", result[i]);
        strcat(result, buffer);

    }
    sprintf(buffer, "%d\n", result[result[0]]);
    strcat(result, buffer);

    return result;
}

It is most likely that the issue occurred in the primeFactorsAsString Function. I don't know where though. If its important, here is also the run.sh script that i made:

gcc main.c -lm
[ $? -eq 0 ] && ./a.out || echo "Compilation failed!"

CodePudding user response:

Turns out I didn't initialize factors before the if statement, switched both of them and now I get just zero as output. That's not what i want but not the error either therefore a solution.

Edit: I found out the second issue as well. I referenced the wrong array in the for loop. Here is the working function:

char* primeFactorsAsString(int n) {
    int *factors;

    factors = primeFactors(n);

    if (factors[0] > 25) {
        return "TO BIG";
    }
    static char result[100];
    char buffer[100];
    for (int i = 1; i < factors[0]; i  )
    {
        sprintf(buffer, "%d * ", factors[i]);
        strcat(result, buffer);
        

    }
    sprintf(buffer, "%d\n", factors[factors[0]]);
    strcat(result, buffer);

    return result;
}

CodePudding user response:

printf(primeFactorsAsString(number)); -- needs format specifier %s, the function returns string.

sprintf(buffer, "%d\n", result[result[0]]); -- result[0] returns a char (as defined by static char result[100]; so you can't index an array with a char.

int *factors; -- is not initialized, always initialize your pointers to NULL.

if (factors[0] > 25) -- factor[0] is out of scope. maybe you would want to declare it as a global variable to be able to access it within the primeFactorsAsString function.

  • Related