Thread: problem returning struct member from function

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    problem returning struct member from function

    I have a binary search tree made up of struct tree_st nodes. The struct has members TData (which is just a typedefed int) and pointers to left and right trees. Everything is working fine; test program takes int values and puts them in the tree and then prints out an in-order traversal.

    But now I'm trying to add findMin and findMax functions. These also work just fine, as long as I print out the min or max in the function. If I try to return the value and print it out in main, I get the address instead! Weird.

    Here's the findMin function:


    Code:
    Tdata 
    findMin(const Tree t) 
    {
      if (t == NULL) return -1;
      if (t->left == NULL)
      {
    	printf("min: %d\n",t->data);       		
    	return t->data;
      }
       else findMin(t->left);
    }

    In main, I do this:
    Code:
    printf("The min value is %d\n", findMin(t));
    (The findMax function does the same but goes to the right.)

    I put the printf in findMin as a test, and sure enough, it prints out the actual integer minimum. But then, returning t->data to main and printing it out prints out an address!!

    Any guidance on this would be much appreciated. Thanks.

  2. #2
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Just a guess: Is t->data of type TData or int ?

    Have you tried int findMin(const Tree t) ? It may be that printf doesn't recognize your data type as an integer. I know that at least cout (c++) would not and print gibberish.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>>>It may be that printf doesn't recognize your data type as an integer.

    If its typedef'ed, I dont think it should be a problem. But thats only a guess. Could you post the entire/relevant code here please?

    Anoop.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Can we see the struct and typedef declarations?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    3

    declarations

    Here are the type declarations:

    Code:
    typedef int Tdata;
    
    typedef struct tree_st
    {
    	Tdata data;
    	struct tree_st * left;
    	struct tree_st * right;
    } * Tree;
    The problem is not the printf; the numbers print out fine in the printf. It's once the number is returned that I get the address... at least, I think it's the address of the value. It's a large integer... don't think random garbage would look like that.

    Haven't tried returning an int because what's the point of a typedef if you can't pass it around!? But, I guess I'll give it a shot.

    thanks

  6. #6
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    just a shot in the dark...
    Code:
    typedef int Tdata;
    
    typedef struct
    {
    	Tdata data;
    	struct tree_st * left;
    	struct tree_st * right;
    } Tree;
    Last edited by Lynux-Penguin; 08-24-2003 at 12:06 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    3
    Ack! Have to "return" the recursive call! Duh. If it wuz a snake it woulda bit me. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with struct?
    By cangel in forum C Programming
    Replies: 8
    Last Post: 09-27-2008, 11:35 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. TCP Header problem (error)
    By nasim751 in forum C Programming
    Replies: 1
    Last Post: 04-25-2008, 07:30 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM