0

Every time I try to run './synthesize jeopardy.wav < songs/jeopardy.txt' it just prints out:

helpers.c:46:17: runtime error: load of null pointer of type 'char'
Segmentation fault

here is my code:

int frequency(string note)
{
int A4 = 440;
char letter[2];
letter[0] = note[0];
letter[1] = '\0';
float steps = 0;
float hertz = 0;
float thertz = 0;

switch (letter[0])
{
    case 'A':
    steps = 0;
    break;

    case 'B':
    steps = 2;
    break;

    case 'C':
    steps = 9;
    break;

    case 'D':
    steps = 7;
    break;

    case 'E':
    steps = 5;
    break;

    case 'F':
    steps = 4;
    break;

    case 'G':
    steps = 2;
    break;

    default:
    return 1;
}

if (atoi(&note[strlen(note) - 1]) < 4)
{
    for (int i = 0; i < atoi(&note[strlen(note) - 1]); i++)
    {
        hertz = 440 * 2;
    }
}
else if (atoi(&note[strlen(note) - 1]) > 4)
{
    for (int j = 4; j < atoi(&note[strlen(note) - 1]); j++)
    {
        hertz = 440 / 2;
    }
}

else
{
    hertz = 440;
}

if (note[1] == '#')
{
    if (note[0] == 'A')
    {
        thertz = round((pow(2, ((steps + 1) / 12))) * hertz);
        return thertz;
    }
    else
    {
        thertz = round(hertz / (pow(2, ((steps - 1) / 12))));
        return thertz;
    }
}
else if (note[1] == 'b')
{
    if (note[0] == 'B')
    {
        thertz = round((pow(2, ((steps - 1) / 12))) * hertz);
        return thertz;
    }
    else
    {
        thertz = round(hertz / (pow(2, ((steps - 1) / 12))));
        return thertz;
    }
}
else
{
    if (note[0] == 'A')
    {
        thertz = round((pow(2, (steps / 12))) * hertz);
        return thertz;
    }
    else
    {
        thertz = round(hertz / (pow(2, (steps / 12))));
        return thertz;
    }
}
return thertz;
}

1 Answer 1

1

You can use note as a char * or as a char[], because it is a string.

So you can use it as an array of char's, or as a pointer to a char.

But you are using it as a pointer to a char[] ...

You should replace code like &note[strlen(note) - 1] by note[strlen(note) - 1]


Apart from that, there is something wrong with your loops.

If you put 440 * 2 in the variable hertz 3 times, it will still contain 880...


Finally, to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

10
  • the line pointed out by the error message is 'letter[0] = note[0];' not the one you pointed out Commented Oct 22, 2018 at 13:33
  • In that case, note must be the null pointer of type 'char' the error is complaining about. So your program apparently calls your frequency() function with a string variable that hasn't been initialized. If the program stops at that point, it will never reach the other 2 errors ... Commented Oct 22, 2018 at 13:51
  • and how do I fix it Commented Oct 22, 2018 at 13:54
  • Find the place where your function was called, and check that the string (or char pointer) actually points somewhere before calling the function ... Commented Oct 22, 2018 at 13:57
  • 1
    I tried looking through the duration part and changed something and it worked, thanks for your help :) Commented Oct 22, 2018 at 15:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .