Thread: Sorting by passing struct as argument

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18

    Sorting by passing struct as argument

    Hi,

    I am writing a program that takes input fr stdin, breaks it into tokens of words, and then count the occurrence of the each word by sorting them into a tree of struct.

    however when i tried to put the words into the struct, it kept giving root a NULL.

    Here's my code:

    Code:
    void sortwords(char **warr, int wc, struct tnode *r){
    	int c=0;
    	for(c=0;c<wc;c++){
    		sort(r, warr[c]);
    	}
    }
    
    
    void sort(struct tnode *r, char *w){    
        if(r == NULL){
            r = (struct tnode *) malloc(sizeof(struct tnode));
            r->word = w;
            r->count = 1;
            r->left = NULL;
            r->right = NULL;
        }else{
        
            int cmp = strcmp(w, r->word);
            if(cmp<0){
                sort(r->left, w);
            }else if(cmp>0){
                sort(r->right, w);
            }else r->count++;
            
        }
    }
    and here's my struct and how i initialized it:

    Code:
    struct tnode {    char *word;
        int count;
        struct tnode *left;
        struct tnode *right;
    };
    
    int main (int argc, const char * argv[]) {
            ...
        struct tnode *root = NULL;
        ...
            //warray is an array of words, checked, no problem
            //wordsread is the number of words in the array
        sortwords(warray, wordsread, root);
        ...
        return 0;
    }
    Last edited by ymc1g11; 10-29-2012 at 02:47 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    void sort(struct tnode *r, char *w){    
        if(r == NULL){
            r = (struct tnode *) malloc(sizeof(struct tnode));
    "r" is local to the function, thus when you change its value the caller doesn't notice it. That's why in sortwords() "r" will always point to NULL.
    You have to pass a pointer to the object when you want that the caller also sees changes to the object. In your case your function prototype should be
    Code:
    void sort(struct tnode **r, char *w);
    and you have to dereference accordingly.
    The same is true for sortwords(). main() won't notice any changes to "root" inside sortwords().

    Bye, Andreas
    Last edited by AndiPersti; 10-29-2012 at 03:11 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    sorted! cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. Argument passing
    By Tarper67 in forum C++ Programming
    Replies: 1
    Last Post: 06-13-2003, 02:59 PM
  4. returning struct and passing as an argument
    By ronin in forum C Programming
    Replies: 4
    Last Post: 06-07-2003, 11:21 AM
  5. argument passing
    By theweirdo in forum C Programming
    Replies: 1
    Last Post: 01-29-2002, 07:42 PM