Thread: accessing struct element from another struct

  1. #1
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17

    accessing struct element from another struct

    Hello, I'm not new to programming. I've been developing games with BASIC (FreeBASIC), and Flash games with ActionScript. I even developed a scripting language for beginning programmers to make games.

    Now, I decided to learn C to be able to make games for handheld consoles.

    I have this code below, where I keep having error message saying:
    error: request for member 'value' is something not a structure or union

    What I wanted to do is be able to have an array of resizeable strings. I'm aware that there's no String datatype in C. It is why I have char *value in struct Foo. It will serve as my resizeable string in my program.

    Now, struct Bar will contain a resizeable array of struct Foo, thus, Foo **foo.

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct {
        
    int etc1;
        
    char *value;
    Foo;

    typedef struct {
        
    char *etc2;
        
    Foo **foo;
    Bar;

    int main() {
        
    Bar bar;
        
        
    bar.foo = (Foo **) malloc(sizeof(Foo) * 2);
            
    bar.foo[0] = (Foo *) malloc(sizeof(Foo));
            
    bar.foo[0].value = (char *) malloc(sizeof("howdy"));
                
    bar.foo[0].value "howdy";
            
            
    bar.foo[1] = (Foo *) malloc(sizeof(Foo));
            
    bar.foo[1].value = (char *) malloc(sizeof("hello world"));
                
    bar.foo[1].value "hello world";
        
                    
    printf("bar.foo.value[0] = %s\n"bar.foo[0].value);
                    
    printf("bar.foo.value[1] = %s\n"bar.foo[1].value);
        
            
    free(bar.foo[0].value);
            
    free(bar.foo[0]);
            
            
    free(bar.foo[1].value);
            
    free(bar.foo[1]);
        
        
    free(bar.foo);
        
        return 
    0;

    In ActionScript, accessing value inside foo would be foo.value. And it follows that accessing foo inside bar would be bar.foo. So I was hoping, I could access value via bar through bar.foo.value. And given that foo is a resizeable array, I was hoping this would work: bar.foo[0].value.

    What am I doing wrong? Is this even possible? Or should I declarare a separate Foo object and pass the pointer to it and assign the value?
    Last edited by creek23; 06-23-2010 at 09:16 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >Now, struct Bar will contain a resizeable array of struct Foo, thus, Foo **foo.
    Shouldn't it be Foo *foo;
    You've an error message but which line???

    Code:
    		bar.foo[0].value = (char *) malloc(sizeof("howdy"));
    		bar.foo[0].value = "hello world";
    This is not really what you want. You are effectively leaking memory.
    I'd suggest you read a good tutorial on C.

  3. #3
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    oops.

    That was just an error in my attempt to simplify what I'm currently working on. It should have been
    PHP Code:
            bar.foo[0].value = (char *) malloc(sizeof("howdy"));
                
    bar.foo[0].value "howdy";
            
            
    bar.foo[1] = (Foo *) malloc(sizeof(Foo));
            
    bar.foo[1].value = (char *) malloc(sizeof("hello world"));
                
    bar.foo[1].value "hello world"
    Anyway, the error is on all lines trying to access .value.
    Last edited by creek23; 06-23-2010 at 09:18 PM.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    strcpy is what you need. You cannot assign a string to a location like that, however, you could assign a string literal to a pointer -- but I don't think that's what you want to do.

  5. #5
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    whoopie! got it! changed .value to ->value.

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct {
        
    int etc1;
        
    char *value;
    Foo;

    typedef struct {
        
    char *etc2;
        
    Foo **foo;
    Bar;

    int main() {
        
    Bar bar;
        
        
    bar.foo = (Foo **) malloc(sizeof(Foo) * 2);
            
    bar.foo[0] = (Foo *) malloc(sizeof(Foo));
            
    bar.foo[0]->value = (char *) malloc(sizeof("howdy"));
                
    bar.foo[0]->value "howdy";
            
            
    bar.foo[1] = (Foo *) malloc(sizeof(Foo));
            
    bar.foo[1]->value = (char *) malloc(sizeof("hello world"));
                
    bar.foo[1]->value "hello world";
        
                    
    printf("bar.foo.value[0] = %s\n"bar.foo[0]->value);
                    
    printf("bar.foo.value[1] = %s\n"bar.foo[1]->value);
        
            
    free(bar.foo[0]->value);
            
    free(bar.foo[0]);
            
            
    free(bar.foo[1]->value);
            
    free(bar.foo[1]);
        
        
    free(bar.foo);
        
        return 
    0;


  6. #6
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17

    Post

    Quote Originally Posted by Kennedy View Post
    strcpy is what you need. You cannot assign a string to a location like that, however, you could assign a string literal to a pointer -- but I don't think that's what you want to do.
    well, you could. as long as the char pointer has memory allocated of just about the same size of the string value being assigned.

    in it's simplest form:
    PHP Code:
    [code]
    #include <stdio.h>

    int main() {
        
    char *value = (char *) malloc(sizeof("howdy"));
        
    value "howdy";
        
    printf("%s"value);
    }
    [/
    code
    should work. (and it does)

  7. #7
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Quote Originally Posted by Bayint Naung View Post
    >Now, struct Bar will contain a resizeable array of struct Foo, thus, Foo **foo.
    Shouldn't it be Foo *foo;
    hmm... let me take a look at it again. thanks for the tip.
    Last edited by creek23; 06-23-2010 at 10:47 PM.

  8. #8
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Thanks Bayint Naung. I edited the code and it does work with only Foo *foo and not Foo **foo, thus no need to malloc both array instance.

    PHP Code:
    [code]
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct {
        
    int etc1;
        
    char *value;
    Foo;

    typedef struct {
        
    char *etc2;
        
    Foo *foo;
    Bar;

    int main() {
        
    Bar bar;
        
        
    bar.foo = (Foo *) malloc(sizeof(Foo) * 2);
            
    bar.foo[0].value = (char *) malloc(sizeof("howdy"));
                
    bar.foo[0].value "howdy";
            
            
    bar.foo[1].value = (char *) malloc(sizeof("hello world"));
                
    bar.foo[1].value "hello world";
        
                    
    printf("bar.foo[0].value = %s\n"bar.foo[0].value);
                    
    printf("bar.foo[1].value = %s\n"bar.foo[1].value);
        
            
    free(bar.foo[0].value);
            
            
    free(bar.foo[1].value);
        
        
    free(bar.foo);
        
        return 
    0;
    }[/
    code

  9. #9
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    Thanks Bayint Naung. I edited the code and it does work with only Foo *foo and not Foo **foo, thus no need to malloc both array instance.

    PHP Code:
    [code]
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct {
        
    int etc1;
        
    char *value;
    Foo;

    typedef struct {
        
    char *etc2;
        
    Foo *foo;
    Bar;

    int main() {
        
    Bar bar;
        
        
    bar.foo = (Foo *) malloc(sizeof(Foo) * 2);
            
    bar.foo[0].value = (char *) malloc(sizeof("howdy"));
                
    bar.foo[0].value "howdy";
            
            
    bar.foo[1].value = (char *) malloc(sizeof("hello world"));
                
    bar.foo[1].value "hello world";
        
                    
    printf("bar.foo[0].value = %s\n"bar.foo[0].value);
                    
    printf("bar.foo[1].value = %s\n"bar.foo[1].value);
        
            
    free(bar.foo[0].value);
            
    free(bar.foo[1].value);
        
        
    free(bar.foo);
        
        return 
    0;
    }
    [/
    code

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Look, you have a memory leak. You are assigning a string literal over a malloced pointer. You are loosing that memory. If you assign a string literal to a pointer, it gets the ADDRESS of that memory, not a strcpy of that memory.

    edit:
    Code:
    root@fw03:~# vim test.c
    root@fw03:~# gcc -os test.c
    root@fw03:~# ./s
    st = 80484e0, tmp = 804a008
    root@fw03:~# cat test.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
            char *st, *tmp;
    
            st = malloc(100);
    
            tmp = st;
            st = "Hello World";
    
            printf("st = %x, tmp = %x\n", st, tmp);
            free(tmp);
            return 0;
    }
    Last edited by Kennedy; 06-24-2010 at 12:45 AM.

  11. #11
    Registered User creek23's Avatar
    Join Date
    Jun 2010
    Location
    Philippines
    Posts
    17
    cool!

    thanks for correcting me. and for a snippet on testing memory leaks.

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem accessing the struct object.
    By TheUmer in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 04:23 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Address of Structure element, and struct malloc
    By HellsChicken in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 02:41 PM

Tags for this Thread