Assign struct member value to struct member value

This is a discussion on Assign struct member value to struct member value within the C Programming forums, part of the General Programming Boards category; I'm having a little trouble understanding how to do this properly. It seems like it's something simple that I have ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    USA
    Posts
    5

    Assign struct pointer array member value to struct pointer array member value

    I'm having a little trouble understanding how to do this properly. It seems like it's something simple that I have just glossed over.

    Here are the struct definitions in my header...

    Code:
    struct source {
    	int num;
    	int *av[128];
    	char *tcpip[128];
    	int *tcpport[128];
    	char *label[256];
    	int *active[128];
    	char *sdip[128];
    	char *hdip[128];
    	char *aip[128];
    }source;
    
    struct display {
    	int num;
    	char *tcpip[128];
    	int *tcpport[128];
    	int *src[128];
    }display;
    Here is the line in which I'm trying to assign values where mon_selected is a simple int used to index a selected monitor.

    Code:
    drag_label(video_bg, source.label[display.src[mon_selected]], samp.x, samp.y, oldx, oldy);
    ...aaaand the error that I can't seem to get rid of without assigning display.src[mon_selected] to a temporary variable. ick.

    Code:
    src/touchscreen.c:57: error: array subscript is not an integer
    It looks to me like I am not dereferencing the display.src member properly. My guess is that it is not following the pointer to the actual value. I'm not sure how to fix it though.

    Enlighten me! Please and thank you.
    Last edited by thahemp; 10-13-2010 at 08:32 AM. Reason: Edited title for clarity

  2. #2
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    When these kinds of problems happen, I like to write small programs to try and figure out why the issue is happening. For instance:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct display{
            int *src[10];
    }display;
    
    int main(void) {
            int i = 0;
            int test = 5;
    
            for(i=0; i<10; i++){
                    display.src[i] = malloc(sizeof(int));
                    *display.src[i] = i;
                    printf("%d\n", *display.src[i]);
            }
    
            *display.src[test] = 15;
    
            printf("\n");
    
            for(i=0; i<10; i++)
                    printf("%d\n", *display.src[i]);
    
            return 0;
    }
    When I originally wrote this, I removed the astrix on the red line to see if I could reproduce your compiler error. When I did this, I received the same error. I then compiled the program with the astrix and everything worked properly.

    P.S. I'm not checking the return value of malloc or free'ing my malloced memory at the end of the program for simplicity sake. You should always do these things as they are good practice.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    USA
    Posts
    5
    Adding the astrix actually makes it compile, but not function. Signal 11 from the kernel.
    Last edited by thahemp; 10-13-2010 at 08:46 AM. Reason: update

  4. #4
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Well, if your not sure why the astrix fixed it, then I haven't helped you.

    What was the index of source.label[] looking for? And what were you giving it?
    What did the astrix do when you used it with display.src[mon_selected]?

    EDIT: I just saw your reply.....if your getting a segfault, it sounds to me like you haven't set aside any memory for the data. You can reference my previous example to see what I mean.

    Last edited by matrixx333; 10-13-2010 at 09:01 AM.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    USA
    Posts
    5
    Alright... what have we learned here.

    1) malloc your memory...
    2) dereference your pointers when assigning values

    Changes in red.

    Code:
    for(i = 0; i < display.num; i++) {
        display.src[i] = malloc(sizeof(int));
        *display.src[i] = -1;
    }
    Code:
    if((!ret) && (dragging == 1) && (check(samp.x, samp.y) == 3)) {
        if(src_selected > -1) {
            *display.src[0] = src_selected;
            activate_display(0, src_selected);
            clean_loop();
            return 0;
        }
    ...
    Code:
    drag_label(video_bg, source.label[*display.src[mon_selected]], samp.x, samp.y, oldx, oldy);
    All works as expected now. Thanks for dose of enlightenment.

  6. #6
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Glad I could help!

    Again, I would recommend checking the return value of malloc() to make sure you don't run out of memory and also remember to free your malloc'ed memory when your done using it.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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