Segmentation fault! Why?

This is a discussion on Segmentation fault! Why? within the C Programming forums, part of the General Programming Boards category; here-s a program i just can not get to work! Please help i`ve got exam in 2 days :-/ Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    Segmentation fault! Why?

    here-s a program i just can not get to work! Please help i`ve got exam in 2 days :-/

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define max 100
    
    typedef struct drvo
    {
    	int broj;
    	struct drvo* levo,*desno;
    }cvor;
    
    cvor* napravi(int br)
    {
    	cvor* novi=(cvor*)malloc(sizeof(cvor));
    	if(novi==NULL)
    	{
    		fprintf(stderr,"greska prilikom alokacije memorije");
    		exit(1);
    	}
    	novi->broj=br;
    	novi->levo=NULL;
    	novi->desno=NULL;
    	return novi;
    }
    
    cvor* ubaci(cvor* drvo, int br)
    {
    	if(drvo==NULL)
    	{
    		
    		return napravi(br);
    	}
    	else if(drvo->broj>br)
    		drvo->levo=ubaci(drvo->levo,br);
    	else if(drvo->broj<br)
    		drvo->desno=ubaci(drvo->desno,br);
    	
    }
    void ispisi(cvor* drvo)
    {
    	if(drvo!=NULL)
    	{
    		
    		ispisi(drvo->levo);
    		printf("%d",drvo->broj);
    		ispisi(drvo->desno);
    		
    	}
    }
    void oslobodi(cvor* drvo)
    {
    	if(drvo!=NULL)
    	{
    		oslobodi(drvo->levo);
    		oslobodi(drvo->desno);
    		free(drvo);
    	}
    }
    
    void pronadji(cvor* drvo, int x)
    {
    if(drvo==NULL)
    printf("element nije pronadjen \n");
    	else
    	{
    		if(drvo->broj==x)
    		printf ("elemen je u drvetu \n");
    		else if(drvo->broj<x)
    		
    			pronadji(drvo=drvo->desno,x);
    		else 
    			pronadji(drvo=drvo->levo,x);
    	}
    	
    }
    int prebroji(cvor* drvo)
    {
    	
    	if(drvo==NULL)
    	return 0;
    	else
    	return
    		prebroji(drvo->levo)+1+
    		prebroji(drvo->desno);
    }
    int br_listova(cvor*drvo)
    {
    	if (drvo==NULL)
    	return 0;
    	else if((drvo->levo==NULL) && (drvo->desno==NULL))
    	return 1;
    	return br_listova(drvo->levo)+br_listova(drvo->desno);
    }
    
    main(int argc, char argv[])
    {
    	int f,j, i=0;
    
    	
    	int a[max];
    
    	FILE *in;
    		if(argc>=2)
    		{
    			in=fopen("argv[1]","r");
    			if(in==NULL)
    			{
    				fprintf(stderr,"greska prilikom otvaranja datoteke %s za citanje",argv[1]);
    				exit(1);
    			}
    			
    		}
    		else
    		{
    			fprintf(stderr,"nedovoljan broj argumenata \n");
    			exit(1);
    		}
    		
    	while((f=fgetc(in))!=EOF)
    		if(isdigit(f))
    		{
    			a[i]=f;
    			i++;
    		}
    
    	cvor* drvo=NULL;
    	printf("blabla");
    	for(j=0;j<5;j++)
    		{
                    drvo=ubaci(drvo,a[j]);
    
                    }
    
    	ispisi(drvo);
    	putchar('\n');
    	pronadji(drvo,1);
    	printf("broj cvorova: %d \n",prebroji(drvo));
    	printf("broj listova: %d \n",br_listova(drvo));
    	oslobodi(drvo);
    	fclose(in);
    }
    Thanks in advance

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Take a look at correct main definitions (I'm 99&#37; certain this is causing bugs in the code):
    http://cpwiki.sourceforge.net/Implicit_main

    >>in=fopen("argv[1]","r");
    This will not do what you want.
    Last edited by Elysia; 06-25-2008 at 06:45 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > a[i]=f;
    You probably want this to be:
    a[i] = f - '0';

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >cvor* ubaci(cvor* drvo, int br)
    >{
    >	if(drvo==NULL)
    >	{
    >		
    >		return napravi(br);
    >	}
    >	else if(drvo->broj>br)
    >		drvo->levo=ubaci(drvo->levo,br);
    >	else if(drvo->broj<br)
    >		drvo->desno=ubaci(drvo->desno,br);
    >	
    >}
    You need a return drvo at the end of this function:
    Code:
    cvor* ubaci(cvor* drvo, int br)
    {
    	if(drvo==NULL)
    	{
    		
    		return napravi(br);
    	}
    	else if(drvo->broj>br)
    		drvo->levo=ubaci(drvo->levo,br);
    	else if(drvo->broj<br)
    		drvo->desno=ubaci(drvo->desno,br);
    	
    	return drvo;
    }

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    2
    Yeah that done the trick

    Thank you, it's aliiiiiiiiiveeeeeeeeeeeeee))

    see ya...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21