Program does not execute

Hallo.

I wrote some code for learning C. Still a beginner.
The problem is if the problem line is active I get no output of the program.
When I comment it out the program works.
I am using Visual Code and gcc/MingW64 in Windows 10 as the compiler. Don't know how to debug in Visual Code. I still have to learn.

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>


struct scale
{
    char NoteNames[7][4];
    char* ScaleName;
    
};


int main(int argc, char **argv)
{
    struct scale CMaj;
    struct scale CMin;
        
    strcpy(CMaj.ScaleName,"C Major");
    strcpy(&CMaj.NoteNames[0][0],"C");
    strcpy(&CMaj.NoteNames[1][0],"D");
    strcpy(&CMaj.NoteNames[2][0],"E");
    strcpy(&CMaj.NoteNames[3][0],"F");
    strcpy(&CMaj.NoteNames[4][0],"G");
    strcpy(&CMaj.NoteNames[5][0],"A");
    strcpy(&CMaj.NoteNames[6][0],"B");

    strcpy(CMin.ScaleName,"C Minor"); //THIS LINE STOPS THE PROGRAM FROM EXECUTING
    strcpy(&CMin.NoteNames[0][0],"C");
    strcpy(&CMin.NoteNames[1][0],"D");
    strcpy(&CMin.NoteNames[2][0],"D#");
    strcpy(&CMin.NoteNames[3][0],"F");
    strcpy(&CMin.NoteNames[4][0],"G");
    strcpy(&CMin.NoteNames[5][0],"G#");
    strcpy(&CMin.NoteNames[6][0],"A#");
    
// -----------------------------------------------------------------    
    printf("\nScale: %s\n",CMaj.ScaleName);
    for(int i=0;i<7;++i)
    {
        printf("%s ",&CMaj.NoteNames[i][0]);

    }
    printf("%s\n",&CMaj.NoteNames[0][0]);
// -----------------------------------------------------------------
    printf("\nScale: %s\n",CMin.ScaleName);
    for(int i=0;i<7;++i)
    {
        printf("%s ",&CMin.NoteNames[i][0]);
    }
    printf("%s\n",&CMin.NoteNames[0][0]);



    return 0;
}
<strings.h> should be <string.h>

ScaleName needs to be an array of char, not just a pointer to char.

Instead of &CMaj.NoteNames[0][0] is should be CMaj.NoteNames[0].
Same for all the others like that.

Also, your C Minor scale notes are incorrect.
Remember that a diatonic scale uses each letter, so instead of
C D D# F G G# A# C

it should be
C D Eb F G Ab Bb C


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Scale
{
    char NoteNames[7][4];
    char ScaleName[20];
};

int main()
{
    struct Scale CMaj, CMin;
        
    strcpy(CMaj.ScaleName, "C Major");
    strcpy(CMaj.NoteNames[0], "C");
    strcpy(CMaj.NoteNames[1], "D");
    strcpy(CMaj.NoteNames[2], "E");
    strcpy(CMaj.NoteNames[3], "F");
    strcpy(CMaj.NoteNames[4], "G");
    strcpy(CMaj.NoteNames[5], "A");
    strcpy(CMaj.NoteNames[6], "B");

    strcpy(CMin.ScaleName, "C Minor");
    strcpy(CMin.NoteNames[0], "C");
    strcpy(CMin.NoteNames[1], "D");
    strcpy(CMin.NoteNames[2], "Eb");
    strcpy(CMin.NoteNames[3], "F");
    strcpy(CMin.NoteNames[4], "G");
    strcpy(CMin.NoteNames[5], "Ab");
    strcpy(CMin.NoteNames[6], "Bb");
    
    printf("\nScale: %s\n", CMaj.ScaleName);
    for (int i = 0; i < 7; ++i)
        printf("%s ", CMaj.NoteNames[i]);
    printf("%s\n", CMaj.NoteNames[0]);

    printf("\nScale: %s\n", CMin.ScaleName);
    for (int i = 0; i < 7; ++i)
        printf("%s ", CMin.NoteNames[i]);
    printf("%s\n", CMin.NoteNames[0]);

    return 0;
}

Thank you dutch. This was very helpful. The thing is what I forgot to say I get an error message for every strcpy(). Even with the new code. I have the feeling I made a wrong installation of MingW64. Can you say what this Warning means?

1
2
3
In file included from .\main.c:3:
C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/x86_64-w64-mingw32/include/string.h:61:18: note: expected 'char * restrict' but argument is of type 'char'
   char * __cdecl strcpy(char * __restrict__ _Dest,const char * __restrict__ _Source);


I found the error. Forgot to make the NoteNames 2-dimensional.
Last edited on
Don't use MingW with Visual Studio. It is possible but at a beginner level you won't gain anything. CL (the default with Visual Studio) is very good compiler in it's own right, in fact I use it with Code Blocks.
By "Visual Code" I assume OP means Visual Studio Code, this amazingly slow text editor
https://code.visualstudio.com/
Topic archived. No new replies allowed.