trouble with pointers

Hy there! Its my first time here, because i have not had a problem like this before. The program has a problem when i run it. When i choose "natakar" the second time and input the information it crashes. There is probably a problem with pointers or something in the function "elt natakar" but i do not know where, so if somebody could help me it would be a blessing;)
my code:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>

#define MIZ 77

typedef struct{
        int miza;
        char natakar[10];
        char kuhar[10];
        char jed[10];
        SYSTEMTIME str_t;
        int kolicina;
        int status;        //1...vneseno   2...pripravljeno
        }narocilo;
        
typedef struct element{
        narocilo narocilnica;
        struct element *naslednji;
        }elt;

elt *natakar(int *st){
     elt *prvi;
     elt *tmp;
     elt *pom;
 
     int i=*st;
     char ime[10];
     char jed1[10];
     
     system("CLS");
     tmp=(elt*)malloc(1*sizeof(elt));
     pom=(elt*)malloc(1*sizeof(elt));
     int temp=-1;
     while((temp<1)||(temp>77)){
                                system("CLS");
                                printf("\tMeni NATAKAR:\n\n");
                                printf("Miza: ");
                                scanf("%d",&temp);
                                }
     tmp -> narocilnica.miza = temp;
     system("CLS");
     printf("\tMeni NATAKAR:\n\n");
     printf("Vase ime: "); 
     scanf("%s", ime);
     strcpy(tmp -> narocilnica.natakar, ime);
     strcpy(tmp -> narocilnica.kuhar, "?");
     system("CLS");
     printf("\tMeni NATAKAR:\n\n");
     printf("Kaj bodo jedli: "); 
     scanf("%s", jed1);
     strcpy(tmp -> narocilnica.jed,jed1);
     temp=-1;
     while ((temp<1)||(temp>20)){          
                           printf("Koliko jedi: ");
                           scanf("%d",&temp);
                           system("CLS");
                           }
     tmp -> narocilnica.kolicina = temp;
     tmp -> narocilnica.status = 1;
     tmp -> naslednji = NULL;
     GetSystemTime(&tmp -> narocilnica.str_t);
     
     if(i==0){
               prvi=tmp;
               prvi -> naslednji = NULL;
               }
     else{
          pom = prvi;
          while(pom -> naslednji != NULL){
                                         pom = pom -> naslednji;
                                         }
          printf("konec zanke");
          pom -> naslednji = tmp;
          }
          
     *st=*st+1;
     return prvi;
     system("PAUSE");
     }
     
void kuhar(int *st, elt *prvi){
     int i=*st;
     int x=-1;
     system("CLS");
     printf("\tMeni KUHAR:\n\n");

     if(i==0){
               printf("Trenutno se ni nobenih narocil.\n\n");
               }
     else{
          elt *pom;
          pom = (elt*)malloc(1*sizeof(elt));
          pom = prvi;
          printf("Pripravimo se na kuhanje...\n\n");
          printf("NAROCILA: \n\n");   //dejmo izpisat vsa narocila
          while (pom != NULL){
                                           printf("\nMiza: %d", pom -> narocilnica.miza);
                                           printf("\nJed: %s * %d",pom -> narocilnica.jed, pom -> narocilnica.kolicina);
                                           if(pom -> narocilnica.status == 1) printf("\nStatus: SE NI SKUHANO");
                                           if(pom -> narocilnica.status == 2) printf("\nStatus: Skuhal %s", pom->narocilnica.kuhar);
                                           printf("\nMizo je stregel %s", pom -> narocilnica.natakar);
                                           printf("\nDatum: %d.%d.%d \n\n",pom -> narocilnica.str_t.wDay,pom -> narocilnica.str_t.wMonth,pom -> narocilnica.str_t.wYear);
                                          
                                           pom = pom -> naslednji;
                                           }
          }
     
     system("PAUSE");
     }

void meni(){
	 int x=-1;	
     elt *prvi;

	 int st=0;    //premikam se po tabeli
	 
	 while (x!=0){ 
		system("CLS");
		printf("\nIzbirajte:\n\t(1) Natakar\n\t(2) Kuhar\n\t(3) EXIT\n");
		printf("\n\tIzbira: ");	
        scanf("%d",&x);
        if(x==1) prvi=natakar(&st);
        else if(x==2) kuhar(&st, prvi);
		}
     free(prvi);
     }

int main(){
    meni();
    return 0;
    }
Anybody?
1
2
     return prvi;
     system("PAUSE");


Anything after a return statement is ignored, if in the same scope. Your system("PAUSE"); will never be used.

Anyway, what does prvi point to?
If it points to a local variable, keep in mind that local variables are lost when their function returns. So you'll get a pointer to an invalid address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char * boom()
{
    char temp = 'a';
    return &temp;
    // bad! when this function ends, temp won't exist anymore, so an address to it will be invalid.
}

char * okay()
{
    static char temp = 'a';
    return &temp;
    // acceptable. static variables behave as if they're global variables, just not at global scope.
    // temp will be "alive" for as long as the program itself runs.
}

prvi points out the first pointer, so taht i do not loose the beginning of pointers.
1
2
          pom = (elt*)malloc(1*sizeof(elt));
          pom = prvi;


The first pom will be lost.
Topic archived. No new replies allowed.