Thread: Problem passing data between functions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    Problem passing data between functions

    I have tried to create a binary tree structure for storing data, but I can't seem to get the program to compile. It flags up an error when returning a from the function createtree and when assigning this result to a in the main program.

    They are both data type tree which is created at the start, and both are pointers. I cannot understand why the program cannot return/assign these two values.

    It is probably something very simple but I would be very grateful for the help.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct tree{
      char thisnode[50];
      struct tree *leftnode;
      struct tree *rightnode;
    }Tree;
    
    Tree *makenode(char *in, Tree *l, Tree *r){
      Tree *t = malloc(sizeof(Tree));
      t->leftnode = l;
      t->rightnode = r;
      strncpy(t->thisnode, in, 9);
      return t;
    }
    
    Tree *insertnode(Tree *root, char *data){
      if (root == NULL) {
        root = makenode(data, NULL, NULL);}
      else if(strcmp(data, root->thisnode)<0){
        root->leftnode = insertnode(root->leftnode, data);}
      else{
        root->rightnode = insertnode(root->rightnode, data);}
      return root;
    }
    
    char *search(Tree *root, char *number){
      if (root == NULL){
        return "Not Found";}
      else if(strcmp(number, root->thisnode) == 0){
        return root->thisnode;}
      else if(strcmp(number, root->thisnode) < 0){
        return search(root->leftnode, number);}
      else {
        return search(root->rightnode, number);
      }
    }
    
    Tree createtree(){
         char i[25];
         int j;
         Tree *a = NULL;
         while (i[0] != '.'){
               for(j = 0; j < 25; j++){
                     i[j] = getchar();
                     a = insertnode(a,i);                 
               }
         }
         return a;
    }
    
    int main()
    {
        Tree *a = NULL;
        a = createtree();
        
        if(a != 0) { 
        printf("\n");
        while(a->leftnode != 0) {
            printf("%s", a->thisnode);
            a = a->leftnode;
        }
        printf("%s", a->thisnode);
    }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    Tree createtree(){
    Code:
         Tree *a = NULL;
    Code:
    return a;
    notice anything wrong?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    oh god, I can't believe I was that stupid.

    Thanks a lot for the help

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    also I'm not seeing how this is gonna do what you want
    Code:
         while (i[0] != '.'){
               for(j = 0; j < 25; j++){
                     i[j] = getchar();
                     a = insertnode(a,i);                 
               }
         }
    i is an uninitialized array, yet you test it against '.'
    then the way I see it, if the user doesn't enter . at the first iteration. this will never stop untill on the 25th, 50th, 75th ... 25*ith (I think that math is right) iteration a . is entered. is this intended?

    ::edit nope that math is wrong...its 26th, 52nd, 78th, 26*ith...I think thats right now
    Last edited by sl4nted; 12-10-2006 at 02:53 PM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    yeh I know thats the next problem I discovered, i've replaced the for loop with a second while loop.

    Although it doesn't seem to exit this loop, Does getchar() scan newline characters in? if not thats where my next problem lies. I have included the modified function createtree below just incase it helps.

    Code:
    Tree createtree(){
         
         char i[50];
         int j;
         Tree *a = NULL;
         while (i[0] != '.'){
               j = 0;
               while (i[j] != '\n'){
                     i[j] = getchar();  
                     j++;              
               }
         a = insertnode(a,i); 
         }
         return *a;
    }

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    the way you have it yes
    ie.
    if you had
    Code:
    for (i = 0; i < some size; i++)
    {
          array[i] = getchar();
    }
    every other position would be newline

    edit:: when exactly do you want it to stop taking new characters? when . is entered?
    Last edited by sl4nted; 12-10-2006 at 03:15 PM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    60
    yes, I want it to place each line into a different node on the tree and then stop when a '.' character is entered.

    edit:

    I have changed the code again so it now exits the loop, although I now get a segmentation fault when I try to return *a to the main function.

    I know this is a memory issue, I have allocated the space for the tree in the *makenode function so I am confused as to what the problem is.

    Here is the new code

    Code:
    Tree createtree(){
         
         char i[50];
         int j;
         Tree *a = NULL;
         while (i[0] != '.'){
               j = -1;
               while (i[j] != '\n'){
                     j++; 
                     i[j] = getchar();  
                                  
               }
         a = insertnode(a,i); 
         }
         return *a;
    }
    Last edited by manutdfan; 12-10-2006 at 03:33 PM.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Look the function signature, it isn't returning a Tree * as you expect.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    you could use a for loop then say
    ch[i] = getchar();
    if ch[i] != '.' then insert
    else break;
    flush stdin (the correct way of course)

    I think that would work. But someone else probably has a really neat way to do this. I'm sure there is one, I just can't think of it.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    hmmm, I think I'm a little confused after looking over this again. each node contains a string? but your trying to insert nodes after every character you get.
    is a node supposed to just contain 1 char? or a string

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. passing data
    By vnrabbit in forum C Programming
    Replies: 4
    Last Post: 11-20-2002, 03:19 PM
  4. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM