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.
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.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;
}
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?