Thread: Problem Allocating Memory For Structure Within Structure In A Function

  1. #1
    Registered User
    Join Date
    Apr 2010

    Problem Allocating Memory For Structure Within Structure In A Function

    Here's the structs for my window(s):

    struct _display {
            int top_limit;
            int bottom_limit;
            int current;
            int max_y;
            int max_x;
            int total;
    typedef struct _sub_window {
            WINDOW *win;
            struct _display *display;
    } Sub_window;
    typedef struct _wnode {
            WINDOW *win;
            struct _sub_window sub_window;
            struct _display *display;
            struct _wnode *next;
            struct _wnode *prev;
    } Windownode;
    Within my main Windownode, I found the need to have another derived window within it, so I'm trying to add that derived window with this...

    int init_sub_window(Windownode **node, int sub_rows, int sub_cols, int start_y, int start_x)
            WINDOW *derived_window;
            struct _display *local_display;
            Sub_window *temp;
            temp = safemalloc(sizeof(Sub_window));
            local_display = safemalloc(sizeof(struct _display));
            /* TODO: Safe malloc for derived windows. */
            derived_window = derwin((*node)->win, (*node)->display->max_y - sub_rows, (*node)->display->max_x - sub_cols, \
                    start_y, start_x);
            if(!derived_window) {
                    fprintf(stderr, "Error in derived window allocation!");
            temp->win = derived_window;
            temp->display = local_display;
            getmaxyx(temp->win, temp->display->max_y, temp->display->max_x);
            temp->display->top_limit = 0;
            temp->display->bottom_limit = temp->display->max_y;
            temp->display->current = 0;
            temp->display->total = 0;
            (*node)->sub_window = temp; 
    In main, I'm calling the function like this...
    init_sub_window(&win_parent[MAIN], 4, 2, 1, 1);
    But, the error I'm getting for this setup is...

    gcc -g3 -c main.c 
    gcc -g3 -c color.c 
    gcc -g3 -c print.c 
    gcc -g3 -c tree.c 
    gcc -g3 -c window.c 
    window.c: In function 'init_sub_window':
    window.c:81: error: incompatible types in assignment
    make: *** [window.o] Error 1
    Line 81 is that last line, (*node)->sub_window = temp;. Within the function, I've tried *node->sub_window, (*node)->sub_window, and *(node)->sub_window, but no success. Where I get the derived window part of the function, where I have (*node)->win and all, I don't get any errors, but that was after tinkering with the parentheses and asterisk. I don't think I understand why those take but the (*node)->sub_window at the bottom doesn't. What I had thought about doing was passing the parent window's Sub_window AND the entire parent window's structure so that I could get it's max_y and max_x, but that seems to be overkill. What am I doing wrong? Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    sub_window is an actual object. In that case, no room need be allocated for it; it is already there. If you feel you want/need dynamic allocation, then sub_window should be a pointer instead.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    Hint: temp is a pointer and sub_window, the member of the struct is not.

  4. #4
    Registered User
    Join Date
    Oct 2008
    "(*node)->sub_window" is of type Sub_Window, where temp is of type Sub_Window*.
    Maybe you intended (*node)->sub_window = *temp;?

  5. #5
    Registered User
    Join Date
    Apr 2010
    Oh, snap! Thanks claudiu! I didn't see that one!!! Aaaah! I meant to make that a pointer! It works! Thank you both!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM