1 Replies - 7880 Views - Last Post: 01 April 2013 - 04:04 AM

#1 comp112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-April 13

Problem with pointers

Posted 01 April 2013 - 03:14 AM

Hy there!
I have been writing a program, it's not in english language, so i hope there will not be a problem. The probelm is when i run it, and choose "natakar" twice, it crashes. The problem must be in the function "elt natakar" but i do not know where, so if anybody knows where the problem is, it would be great...
my code:
#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;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with pointers

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Problem with pointers

Posted 01 April 2013 - 04:04 AM

elt *natakar(int *st){
	elt *prvi; /* this contains garbage */
	/* ... */
	/* if it's the first item, you actually assign it to something
	if(i==0){
		prvi=tmp;
		prvi -> naslednji = NULL;
	} else {
		/* if not, it's still just garbage */
		pom = prvi;



I'd go with something like:
void loadNarocilo(narocilo *) { /* your code here */ }
elt *getLastNode(elt *head) { /* your code here */ }

elt *natakar(elt *head) {
	elt *tmp = malloc(sizeof(elt));
	loadNarocilo(&(tmp->narocilnica));
	tmp->naslednji = NULL;
	
	if(head==NULL){
		head = tmp;
	} else {
		getLastNode(head)->naslednji = tmp;
	}
	return head;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1