• 0

Problem with pointers


Question

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:

[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <Windows.h>[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]#define MIZ 77[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]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;[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]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");[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3] 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");
}[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]void meni(){
int x=-1;
elt *prvi;[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3] 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);
}[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]int main(){
meni();
return 0;
}[/size][/font][/color]
[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3][/CODE]

[/size][/font][/color]

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

What tools/platform are you using. On Windows you can run this in debug mode and it should catch crashes and allow you to debug. The same with gdb on Linux.

With both tools you can set breakpoints and then inspect the variable values. Might be a good idea to do so before the crash happens.

Also note that the -> operator is typically written as "p->attr"

Link to comment
Share on other sites

  • 0

In my own opinion, the best thing to do in situations like this is to start commenting out section by section in your code and then start un-commenting until you find where it crashes.

Start printing out what your code is doing, which point is at etc. For instance: printf("Entering the while loop...\n\r"); printf("Accessing xyz\n\r"); <statement> printf("Accessing something else...\n\r"); and see where it stuffs up...

Link to comment
Share on other sites

  • 0

In my own opinion, the best thing to do in situations like this is to start commenting out section by section in your code and then start un-commenting until you find where it crashes.

Start printing out what your code is doing, which point is at etc.

i have done that, it crashes at this point:


while(pom -> naslednji != NULL){
pom = pom -> naslednji;
}
printf("it does not come to here");
[/CODE]

and i do not know where the problem is...

Link to comment
Share on other sites

  • 0

i have done that, it crashes at this point:


while(pom -> naslednji != NULL){
pom = pom -> naslednji;
}
printf("it does not come to here");
[/CODE]

and i do not know where the problem is...

Well it looks like you have an infinite loop there.

pom -> naslednji != NULL

pom->naslednji[b] never [/b]becomes [b]NULL [/b]therefore the condition always evaluates to [b]true[/b] and the loop never ends. That's why the statement after the loop never runs.

Link to comment
Share on other sites

  • 0

Well it looks like you have an infinite loop there.

pom->naslednji never becomes NULL therefor the loop never ends. That's why the statement after the loop never runs.

how can it not become NULL, i have assigned prvi->naslednji=NULL and then said pom=prvi, so therefore it shoud come to NULL, shouldnt it?

Link to comment
Share on other sites

  • 0

how can it not become NULL, i have assigned prvi->naslednji=NULL and then said pom=prvi, so therefore it shoud come to NULL, shouldnt it?

You think you have but is (i really 0)?


if(i==0)
{
prvi=tmp;
prvi -> naslednji = NULL;
}
[/CODE]

Link to comment
Share on other sites

  • 0

Oh, there you have it. DON'T EVER USE DEVC++ *AHEMM* *AHERR* *cough*... sorry for that. Seriously, though, please don't. Debugger will show you why - it will crash more than your program would.

A basic linked list exercise here, but the variable naming and the occasional formatting voes made it a complete mess to read through.

With that I try to hide my own ignorance on the topic, har har har.

Link to comment
Share on other sites

  • 0

I am programming in Windows 7, in DevC++.

I do not know how to use a debugger...

Could you please help me???

Do yourself a favor and use Visual Studio Express Desktop instead. When you launch your program normally in Visual Studio, (i.e. via the F5 shortcut or Debug->Run), you are automatically using a debugger which will break by itself on the line where the crash is happening and allow you to inspect the value of every variable. You can also very easily step through your code line by line and see exactly what is happening to every variable as the program execute.

DevC++ is apparently not abandoned anymore, there's a single dev working on it, but it's probably still not very good.

  • Like 2
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.