Thread: I print something and at the next line it points to NULL

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    I print something and at the next line it points to NULL

    Code:
    printf("%s \n",treePtr->node->dedomena.name);
    	if (Tree_keno(treePtr)) {    print("2\n");}
    where
    Code:
    int Tree_keno(structPtr treePtr)
    {
     	return (treePtr->node==NULL);
    }
    Output
    Code:
    Kristina
    2
    Can anyone figure out whats wrong?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I'm terribly sorry, but my mind reading machine is in the shop...

    You need to show us at least the part of the program involved in the problem... How you're allocating your pointers and structs, the definition of the struct itself ... anything that might contribute to the error.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I am sorry but i didn't in order not to tire you..
    Definitions
    Code:
    typedef struct TStoixeioyTree{/*TStoixeioyTree.h*/
        char* name;
        char* surname;
    }TStoixeioyTree;
    typedef struct typos_komvou * typos_deikti;/*ch8_BSTpointer.h*/
    
    typedef struct typos_komvou{ /*ch8_BSTpointer.c*/
    	TStoixeioyTree dedomena;
    	typos_deikti apaidi;
    	typos_deikti dpaidi;
    } typos_komvou;
    
    struct tree_struct{/* den exw idea pou pane ta gamidia */
    	typos_deikti node;
    }tree_struct;
    
    typedef struct tree_struct *structPtr;
    Create
    Code:
    structPtr Tree_dimiourgia()
    {
        structPtr tree;
        tree=malloc(sizeof(structPtr));
    	tree->node=NULL;
    	return tree;
    }
    Insert
    Code:
    void Tree_eisagogi(structPtr treePtr , TStoixeioyTree stoixeio, int *error)
    {
        typos_deikti origin,temp;
        origin=treePtr->node;
        
    	if(Tree_keno(treePtr))
    	{
    		temp=malloc(sizeof(typos_komvou));
    		if(temp==NULL)
    		{
    			*error=1;
    			return;
    		}
    		treePtr->node=temp;
    		treePtr->node->dedomena.surname=malloc(strlen(stoixeio.surname)+1 );
            treePtr->node->dedomena.name=malloc(strlen(stoixeio.name)+1 );
    		TStree_setValues(&((treePtr)->node->dedomena), stoixeio);
    		treePtr->node->apaidi=NULL;
    		treePtr->node->dpaidi=NULL;
    	}
    	else if(TStree_mikrotero(stoixeio, treePtr->node->dedomena))
    	{
    		treePtr->node=treePtr->node->apaidi;
    		Tree_eisagogi(treePtr , stoixeio, error);
    	}
    	else if(TStree_megalytero(stoixeio, treePtr->node->dedomena))
    	{
            treePtr->node=treePtr->node->dpaidi;
    		Tree_eisagogi(treePtr , stoixeio, error);
    	}
    	else if(TStree_iso(stoixeio, treePtr->node->dedomena))
    	/* These two lines of code are mine.Without this two,if we had a name twice then function would set error=1*/
    	    return;
    	else
    		*error=1;
    	if(i!=1)            /* i is a global var.*/
    		treePtr->node=origin;
    }
    Print
    Code:
    void nodesprint(structPtr treePtr, FILE *fp)                     /* Print tree nodes */
    {
    	typos_deikti origin;
    	origin=treePtr->node;
    	if (Tree_keno(treePtr)) {                      /* If tree is not empty */
    	treePtr->node=treePtr->node->apaidi;
        nodesprint(treePtr,fp);               /* Print left subtree */
        fprintf(fp,"%s %s\n", treePtr->node->dedomena.surname , treePtr->node->dedomena.name);/* Print root node */
        treePtr->node=treePtr->node->dpaidi;
        nodesprint(treePtr,fp);            /* Print right subtree */
     }
      treePtr->node=origin;
    }
    Empty tree
    Code:
    int Tree_keno(structPtr treePtr)
    {
     	return (treePtr->node==NULL);
    }
    Last edited by std10093; 05-21-2011 at 06:04 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably not directly related, but:
    Code:
    tree=malloc(sizeof(structPtr));
    Not going to work. You need to allocate enough memory for a whole entire struct, not just a pointer to struct.

    Your comments about Tree_keno and the code don't seem to match; you write "If tree is not empty" next to "if (Tree_keno(treePtr))", but that's not the way that test works. I would guess your trashing your tree on a regular basis.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by tabstop View Post
    Probably not directly related, but:
    Code:
    tree=malloc(sizeof(structPtr));
    Not going to work. You need to allocate enough memory for a whole entire struct, not just a pointer to struct.

    Your comments about Tree_keno and the code don't seem to match; you write "If tree is not empty" next to "if (Tree_keno(treePtr))", but that's not the way that test works. I would guess your trashing your tree on a regular basis.
    No,malloc is not the problem because i do i have to do in main.You were right for my Tree_keno error.However i think that the problem is that i do not link the root with its child!So,the problem is in Tree_eisagogi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 PM
  2. Calculating points of a line
    By Iamien in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2004, 07:40 PM
  3. print floating-points
    By lambs4 in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2002, 09:39 AM
  4. Draw Line Between Two Points
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2002, 08:23 AM