Sign in to follow this  

Pointer pointing to same pointer?

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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). 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!)

Share this post


Link to post
Share on other sites
It should update the object since it points to the same memory location. Are you sure that you aren't mistyping it somewhere like

SHELL shell = *tank->turrent->shell;

Well, I find this unlikely.

Perhaps you could run the program with debugger and see what really happens with the pointers. Btw. why don't you use C++ ? I guess that you'd be a bit more comfortable with it since you come from java background.

Another unrelated question, what is the shell thing in the turret thing ? I figure that firing a turret creates a shell but that is the moment when the shell becomes a separate thing. Just asking, perhaps I don't understand your design.

Best regards

Share this post


Link to post
Share on other sites
i'll have another try and the pointers in the morning... gotta bag some sleep bout now...

i'm really thinking i should move to c++ too. i've got no function overloading, i've written the same type of linked list code for each object and written a new struct for each object that practically have the same data. I know there's prolly a way to do it in C, which is why i thought id give it a chance.

and your right, the shell should be moved to its own object, still just mucking around with a few things first though.

Share this post


Link to post
Share on other sites
Quote:
Original post by newioi've written the same type of linked list code for each object


First thing coming to mind is having list elements like
struct {ListElement *next; void* data;};
It even allows you to dump completely unrelated things in the same list (figuring out what to cast it into would require a type-field in all objects though, and the gazillion if's or switches won't help performance)

Quote:
and written a new struct for each object that practically have the same data.


What would be wrong about making a _seperate_ struct for all this data and just make all these objects HAVE one of them? Not as nice, but almost the same result as inheriting.

Share this post


Link to post
Share on other sites
Quote:
Original post by newio i've written the same type of linked list code for each object


you could use void*'s for that you know?:


struct Foo
{
//.. generic data pointer
void* pData;

struct Foo* next;
struct Foo* prev;
};


That's how I did code reuse in C and just type-casted to the appropiate type.
I of course have since moved along to C++ - mainly for code reuse.

Oh.. and about your problem: have you tried printing out the address for the shell variable and the qualified shell data member to see if they're really pointing to the same object?

- xeddiex

Share this post


Link to post
Share on other sites
consider going to c++, its way easyer if u come from a java background (me2) & its more like java than c ... hmm ifi came across that in c++ id just type ... wait ill try it to make sure it works ... :) ... ok here u go a small test prog ... will conpile using devCpp <-- the most common first compiler for newcommers to c++ you can find it at bloodshed.net but this might work in c 2;

#include <iostream>

using namespace std;

struct TPOS { int pos; };
struct TURT { TPOS *tpos; };
struct TANK { TURT *turt; };

int main()
{
TANK *tank = new TANK;
tank->turt = new TURT;
tank->turt->tpos = new TPOS;

tank->turt->tpos->pos = 10; // quickly set this to 10 as a test

cout << tank->turt->tpos->pos << endl; // show that it has been set to 10

TPOS *sec;
sec = tank->turt->tpos;

cout << sec[0].pos << endl; // confirm that they point to the same place!

return 0;
}

also something thats pritty cool if u used pointers to pointers u could point to all your tanks in an array .... take a look at STL ther is a container there called "vector" that might also help u in what ur going ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this