-1

Bear with me as I am relatively new to C.

I am trying to create a function, which could take the names of musical notes (i.e. D or D#) as an argument, and return the value of their corresponding midi note number (midi number table) with a second argument for octave.

However, I seem to be having problems with either the initialisation of the string, or using it within the function. This is the function I wrote:

int midi_number(char note[], int octave)
{
    int midiNumber

    if (note[1] = '#')
    {
        switch(note[0])
        {
        case 'C': case 'c':
        midiNumber = 25;
        break;
        case 'D': case 'd':
        midiNumber = 27;
        break;
        ...
        }
    }
    else
    {
        switch(note[0])
        {
        case 'C': case 'c':
        midiNumber = 24;
        break;
        case 'D': case 'd':
        midiNumber = 26;
        break;
        ...
        }
    }
    return (midiNumber + (octave * 12))
}

I am then calling the function as follows:

midi_number('C#', 1);

Hopefully this gets across the idea of what I'm trying to achieve. I have researched into pointers and arrays, but I can't see how it applies to what I am doing.

Any help massively appreciated!

Harvs
  • 3
  • 2

1 Answers1

0

note[1] = '#' is assigning '#' to note[1], not comparing them.

You need note[1] == '#' inside your if statement.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
  • Thanks! very stupid mistake. That, plus cleaning up the syntax problems has got it working. – Harvs Apr 05 '16 at 17:18