Program keeps crashing, segmentation fault.

My prof gaves us a program to run in dev c++ that deals with pointers to help us get practice for an exam. Whenever I try to run it, though, it keeps crashing and I don't know why. I don't know much about pointers (still a beginner), so i don't know what to look for in the code that causes it to crash. I'm new to this site and was hoping people on here can tell why it's not running. I just need the program to run so i can check the output to see if I'm tracing it correctly. Thanks.

Here is the code he gave us:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct foo{  int num;
             char* word;
           //char word[20];
             struct foo *ptr;};
             
char w1[5]; int r,g,h;
void func1(struct foo);
void func2(struct foo*);
int main() { 
    //char w1[5]; int r,g,h;//int r;int h;int g;
        w1[0]='a'; w1[1]='b'; w1[2]='c'; w1[3]='\0';
        struct foo a,b;
        a.num = 5; a.ptr = &a;
        b.num=80; b.ptr= &a;
        a.word= &r;// we need this line for the weak compilers
        b.word=&h; // we need this line for the weak compilers
        strcpy(a.word,"whichwordgood");
        strcpy(b.word, w1);
         printf("89 is %d %s %s\n", a.num, w1, a.word);
    //     a.word= &w1[0];
// strcpy(a.word,w1);
         printf("80 is %d %s %s\n", a.num, w1, b.word);
        func1(b);//func1(a);
        printf("8 is %d %s\n", a.num, a.word);

        a.num = 100;
      //  a.word = "secondword";
        func2(&a);
        printf("11 %d %s\n", a.num, a.word);
        system("pause");
}                                          
void func1(struct foo a)                  
{      while(*(a.word) != '\0')           
           {                              
                putchar(*(a.word));       
                a.word++;                 
                a.word++;                 
                a.word++;                 
           }                              
        putchar('\n');                    
        a.word--; a.word--; a.word--;     
        printf("2 is %s\n", a.word );     
                                          
        strcpy(a.word, "whatever");       
        if(a.num % 10 != 0)               
           { a.num *= 2; }               
        a.word--;                         
        strcpy(a.word, "whatever");       
        a.word[0]='P';                              
        printf("3 is %d\n", a.num);           
        if( (a).num ==  a.ptr->num)        
          { (a).num = (* (a.ptr)).num +1; } 
        else                                
          { a.num = 200; }                  
        a.ptr->ptr = &a;
        printf("4 is %d %s\n", (a).num, (*((*(a.ptr)).ptr)).word);
        printf("5 is %s\n",  a.word);
        strcpy(a.word,"wordseven");
        printf("6 is %d %s\n", (a).num, a.word);
        printf("7 is %s\n", (*((*((*(a.ptr)).ptr))).ptr).word);
        strcpy(a.ptr->ptr->ptr->word,"wordsix");
}
void func2(struct foo *a)
{       while(*(a->word) != '\0')     //TRY DOING THIS IN EXPANDED NOTATION
           {    putchar(*(a->word));
                a->word++;
           }
        putchar('\n');
        if(a->num % 10 != 0)
           { a->num *= 2; }
        a->word--;
        printf("10 is %d\n", (*a).num);
}

Last edited on
Please put in code tags with correct indentation.
I just did, sorry about that.
Topic archived. No new replies allowed.