Quote:Original post by newio
Hi, i've ran across a little difficulty, i've solved the problem but would like to know why this is for future reference:
Programming in C, im running into some pretty long nested structs:
tank->turrent->shell->mov->vel_x
I figured i could save myself some typing by:
SHELL* shell = tank->turrent->shell;shell->mov->vel_x;
I figured this would be using the same memory address, and therefore the same SHELL struct. But it turns out it makes a new one or something (or at least it wouldn't update the tank->turrent->shell struct).
What you've done is create a new variable (shell) of type pointer to SHELL and assigned it the value from tank->turrent->shell. You should be able to use this new variable to access the values it points to. It looks to me that the problem is that you aren't doing anything with
shell->mov->vel_x;
You need to assign a different value to it, pass it to a function or operate on it in some other way, for example, increment or decrement it.
shell->mov->vel_x++;
shell->mov->vel_x--;
It's also possible that one of these struct member names isn't a pointer but an actual structure and thus it's child members should be accessed using dot notation instead.
tank->turrent->shell.mov->vel_x++;
It really depends on how each of these structures is defined. If the structure contains a pointer to a structure, members of that structure should be accessed through the pointer using -> notation. If the structure contains another structure, members of that structure should be accessed using dot notation.
To use the structures from vbms, with C syntax:
typedef struct tagTPOS { int pos; } TPOS;
typedef struct tagTURT { TPOS *tpos; } TURT;
typedef struct tagTANK { TURT *turt; } TANK;
TANK *tank = malloc(sizeof(*tank));
tank->turt = malloc(sizeof(TURT));
tank->turt->tpos = malloc(sizeof(TPOS));
tank->turt->tpos.pos = position; // what ever value you want to assign
Change those structure definitions a bit and you have to change the notation
typedef struct tagTPOS { int pos; } TPOS;
typedef struct tagTURT { TPOS tpos; } TURT; // notice the change here
typedef struct tagTANK { TURT *turt; } TANK;
TANK *tank = malloc(sizeof(*tank));
tank->turt = malloc(sizeof(TURT));
tank->turt.tpos.pos = position; // what ever value you want to assign
Hope that helps.
Quote:Original post by newio
Is there something im missing here. Im from a Java background so pointers are taking me a lil time to get used to (getting there, but been reading too much, need some straight answers to save my brain!)
Switching to C++ isn't going to resolve your issues with pointers. C++ accomodates oop programming, allowing you to take advantage of your java background, but there are still differences between these languages.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man