scanf to struct pointer

This is a discussion on scanf to struct pointer within the C Programming forums, part of the General Programming Boards category; This is the framework of my program (without the code of functions). It is meant to build a binary tree. ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    scanf to struct pointer

    This is the framework of my program (without the code of functions). It is meant to build a binary tree. My question i about the scanf with pointer to struct. In current code, though it is compiled, it is crushed in the scanf stage when debug it.

    where is my mistake?


    Code:
    struct tree_el {
       int val;
       struct tree_el * right, * left;
    };
    
    typedef struct tree_el node;
    
    void main() {
       node *curr=NULL, *root= NULL;
     
       printf("please enter a number\n");
       scanf ("%d", curr->val);
    
       while (curr->val!=0)
       {
          curr = (node *)malloc(sizeof(node));
          curr->left = curr->right = NULL;
          printf("please enter next number\n");
          scanf ("%d", curr->val);
          insert(&root, curr);
      }

    TIA.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
     scanf ("%d", &curr->val);
    scanf() functions require an address to write to.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > void main() {
    This is your first mistake

    > scanf ("%d", curr->val);
    This is your second - you haven't malloc'ed the space yet

    Gotta wonder how many compiler errors this generated.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah yes, everything Salem said are of more importance since your code has some fundamental errors that should prevent it from really doing anything other than seg faulting. I just saw scanf() in the title and CTRL+F'ed scanf() with the standard address error. I am curious, why do you even have the initial scanf() that resides outside the loop?

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    This is your first mistake
    To go further into it, main should always return an int. It may work for you, but the standard is very clear about it: main returns an int. It will cause problems on other implementations, and also on this board.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    by Salem
    > scanf ("%d", curr->val);
    This is your second - you haven't malloc'ed the space yet
    this solved my original issue


    by sean_mackrory
    To go further into it, main should always return an int. It may work for you, but the standard is very clear about it: main returns an int. It will cause problems on other implementations, and also on this board.
    thanx for notice. I know it, but keep forget...

    by master5001
    I am curious, why do you even have the initial scanf() that resides outside the loop?
    thanx, again for notice. It was a mattar of habit.
    fixed that too.

    and now to the insert function:
    Code:
    void insert(node ** tree, node * item) 
    {
       if(!(*tree)) {
          *tree = item;
          return;
       }
       if(item->val<(*tree)->val)
          insert(&(*tree)->left, item);
       else if(item->val>(*tree)->val)
          insert(&(*tree)->right, item);
    }
    it doesn't build the tree...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > curr->left = curr->right = NULL;
    Did you remember to do this on your first malloc (which you just added) ?

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    curr->left = curr->right = NULL;
    now I did...
    but this doesn't seem to make a difference.
    I have noticed that
    (*tree)->val
    and
    item->val
    have the same value during the function. it doesn't seem healthy, but I think it is the right code. isn't it?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Dunno - post the WHOLE code as it currently stands

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    This is my full code:
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    struct tree_el {
       int val;
       struct tree_el * right, * left;
    };
    
    typedef struct tree_el node;
    
    void insert(node ** tree, node * item) 
    {
       if(!(*tree)) {
          *tree = item;
          return;
       }
       if(item->val<(*tree)->val)
          insert(&(*tree)->left, item);
       else if(item->val>(*tree)->val)
          insert(&(*tree)->right, item);
    }
    
    void printout(node * tree) {
       if(tree->left) printout(tree->left);
       printf("%d\n",tree->val);
       if(tree->right) printout(tree->right);
    }
    
    int main(void) 
    {
       node *curr=NULL, *root= NULL;
    
       curr = (node *)malloc(sizeof(node));
       curr->left = curr->right = NULL;
    
       while (curr->val!=0)
       {
          curr->left = curr->right = NULL;
    	  printf("please enter next number\n");
    	  scanf ("%d", &curr->val);
          insert(&root, curr);
      }
    
       printout(root);
    
       return 1;
    }

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    New nodes need malloc to be called each time. You were using the same node for all inserts.
    Code:
    int main(void) 
    {
      node *curr=NULL, *root= NULL;
    
      do
      {
        curr = (node *)malloc(sizeof(node));
        curr->left = curr->right = NULL;
        printf("please enter next number\n");
        scanf ("%d", &curr->val);
        insert(&root, curr);
      } while (curr->val != 0);
    
      printout(root);
    
      return 0;
    }
    1 as the return for main means failure, 0 means success.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  3. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 06:40 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

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