Thread: accessing struct element from another struct

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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