Skip to main content
GameDev.net gamedev.net
🔒 Locked

pointers to structures

Started by BlackWind Dec 8, 2005 at 5:37 PM 7 replies 650+ views
Original Post
BlackWind
BlackWind
Hi, i have this code: typedef struct ru *ruta; typedef struct ru { char m[3][3]; ruta next; }rute; cand some give me a mini example on how to use it? maybe give "m" and next a value and print them... thanks
CplusplusDEMIGOD
CplusplusDEMIGOD
I'm not exactly sure what you are trying to do, but if I follow you correctly I think this should work just fine for you:

typedef struct ru *ruta;
typedef struct ru
{
char m[3][3];
ruta next;
}rute;

First, if you are programming in C++, use this format instead:

struct rute
{
char m[3][3]
ruta next;
}

Otherwise, if you are just trying to code in C, stick with what you have. Also, try to keep your code indented as I did above. You will thank yourself later! :D

So

typedef struct ru
{
char[3][3];
ruta next;
}rute;

void main()
{
rute* instanceOfRute;
instanceOfRute->char[0] = "A";
instanceOfRute->char[1] = "B";
/* ...etc... */
//then just use printf or cout, to display the results as follows:
cout << instanceOfRute->[0];
}

Needless to say, since it appears that you are using a linked list data structure, you will need to make functions to traverse the list.

I hope this is helpful to you!

:)
xeddiex
xeddiex
typedef struct ru *ruta;typedef struct ru{	char m[3][3];	ruta next;}rute;int main(int argc, char **argv){	rute p;	strcpy(p.m[0], "1");	strcpy(p.m[1], "2");	strcpy(p.m[2], "3");	p.next = &p	printf("%s", p.next->m[0]);	printf("%s", p.next->m[1]);	printf("%s", p.next->m[2]);	getchar();	return 0;}
one..
Inmate2993
Inmate2993
Typedef is more or less just a #define. It has some extra compiler error checking, but its ultimately just an alias. Officially, this is what the specification says to do:
struct foo{  int bar;  struct foo * next;};

After then, if you typedef a foo_t, then you can use it interchangably with struct foo.
typedef struct foo foo_t;foo_t ball;foo_t powder;ball.next = &powder
william bubel
sordid
sordid
Just have to be careful, because of all the stack variables peppering the replies here.

void main(){rute* instanceOfRute;instanceOfRute->char[0] = "A";instanceOfRute->char[1] = "B";/* ...etc... *///then just use printf or cout, to display the results as follows:cout << instanceOfRute->[0];}


That would fail horribly considering 'instanceOfRute' has not actually been assigned any value. Likewise, trying 'instanceOfRute->[0]' seems to be a syntax error because, and somebody correct me if I'm wrong, but neither C or C++ support empty attribute blocks (also known as error C3409)

xeddiex has the right idea, but you have to be careful when you get pointers to stack-based objects such as 'p'. In general, you want to stay away from
	rute p;        // ...	SomeInstanceOfP.next = &p

because 'p' will disappear when you exit that scope.

By the way... where did this code come from?

*cough* source tags *cough*
CplusplusDEMIGOD
CplusplusDEMIGOD
How do you use source tags? I'm kinda new here buddy. Lend a brother a hand.
CplusplusDEMIGOD
CplusplusDEMIGOD
*sheepish*

:D Oh god! I feel salty...

Yes my code is broken but I promise I am not that big of an ass!

I do this for a living (see how easy it is :D, you don't even have to know rudimentary programming lol)

Sorry about that
*/sheepish*
rip-off
rip-off
Quote:
Original post by CplusplusDEMIGOD
How do you use source tags? I'm kinda new here buddy. Lend a brother a hand.


click "edit" on my post to see how i do this :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.