Problem Modifying Struct Member Passed Into Function

This is a discussion on Problem Modifying Struct Member Passed Into Function within the C Programming forums, part of the General Programming Boards category; Struct: Code: typedef struct tnode { int id; char *artist; char *title; char *remixer; char *label; int year; char *genre; ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    Problem Modifying Struct Member Passed Into Function

    Struct:

    Code:
    typedef struct tnode { 
            int id; 
            char *artist;
            char *title; 
            char *remixer;
            char *label; 
            int year;
            char *genre; 
            char *project;
            int mark;
            char *class; 
            int class_number;
            struct tnode *next;
            struct tnode *prev;
    } Treenode;
    I made a head pointer in main, and I set the current pointer to the head:

    Code:
    /* Set tree_current_ptr to the tree head. */
    tree_current_ptr = tree_head_ptr;
    I call this function with the tree_current_ptr so that its position can get modified in the function:

    Code:
    void print_rows(Treenode **start, Windownode *node, float *spaces, int num_rows, int direction)
    {
            int count_rows;
            int pos_y, pos_x=0;
            int limit;
    
            Treenode *cur = *start; 
    
    #ifdef _DEBUG_ 
            curs_set(1);
    #endif
    
            if(direction == PRINT_FWD) {
                    pos_y = 0;
                    limit = node->sub_window->display->max_y;
    
                    for(count_rows=0; cur && (count_rows < num_rows) && (pos_y < limit); ++count_rows, ++pos_y, cur=cur->next) {
                            print_row(node->sub_window, cur, spaces, pos_y, pos_x); 
                            wrefresh(node->sub_window->win);
                    }
    
            } else if (direction == PRINT_BKWD) {
                    pos_y = node->sub_window->display->max_y - 1;
                    limit = 0;
    
                    for(count_rows=0; cur && (count_rows < num_rows) && (pos_y >= limit); ++count_rows, --pos_y, cur=cur->prev) {
                            print_row(node->sub_window, cur, spaces, pos_y, pos_x); 
                            wrefresh(node->sub_window->win);
                    }
            }
    
    #ifdef _DEBUG_ 
            curs_set(0);
    #endif
    
    }
    In main I do:

    Code:
    print_rows(&tree_current_ptr, win_parent[MAIN], spaces, 40, PRINT_FWD);
    However, in the for loop, when I set cur = cur->next, the tree_current_ptr comes out unaltered. My code like this compiles. I tried setting it like (*cur)=cur->next, but that didn't compile. I also tried just passing a pointer to the struct as a parameter, but that didn't work. What am I missing about modifying the struct member?

  2. #2
    Registered User
    Join Date
    Apr 2010
    Posts
    15

    Solved

    I'm simply returning the current pointer from the function. Thanks anyways!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Problem defining structure
    By MTK in forum C Programming
    Replies: 12
    Last Post: 09-08-2009, 03:26 PM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  4. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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