forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to create a pointer to multidimensional array of structs

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
NeoXeno



Joined: 20 Oct 2005
Posts: 10

PostPosted: Mon May 15, 2006 5:54 pm    Post subject: How to create a pointer to multidimensional array of structs Reply with quote

Hi,

I am well under way in creating my first homebrew demo and I can use some help. I just started learning to program in C a few weeks ago so forgive me if this sounds trivial.

Lets say I declare the following:

struct example {
int number;
};

struct example demo[2][3][4];
struct example *ptr;

Is there a way I can make the pointer 'ptr' point to 'demo' so that I can access it's content using 'ptr'?

I thought if I did this

ptr = demo;

that it would point to the location of 'demo' (like how an integer type pointer points to the first element of a single array of integers) so I can do this

ptr[0][0][2].number = 8;

Obviously, this doesn't work, but it should give you an idea of what I'm trying to do. Any suggestions?
Back to top
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Mon May 15, 2006 6:52 pm    Post subject: Reply with quote

struct example ***ptr; // ?
_________________
Shoot Pixels Not People!
Makeshift Development
Back to top
View user's profile Send private message Visit poster's website
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon May 15, 2006 7:12 pm    Post subject: Reply with quote

Drakonite wrote:
struct example ***ptr; // ?


I don't think that would work. The compiler wouldn't know the size of each dimension.
Back to top
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Mon May 15, 2006 7:18 pm    Post subject: Reply with quote

Multidimentional variables are ugly, and as soon as you bring pointers into it you are just causing trouble.
_________________
Shoot Pixels Not People!
Makeshift Development
Back to top
View user's profile Send private message Visit poster's website
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Mon May 15, 2006 7:59 pm    Post subject: Re: How to create a pointer to multidimensional array of str Reply with quote

NeoXeno wrote:
Hi,

I am well under way in creating my first homebrew demo and I can use some help. I just started learning to program in C a few weeks ago so forgive me if this sounds trivial.

Lets say I declare the following:

struct example {
int number;
};

struct example demo[2][3][4];
struct example *ptr;

Is there a way I can make the pointer 'ptr' point to 'demo' so that I can access it's content using 'ptr'?

I thought if I did this

ptr = demo;

that it would point to the location of 'demo' (like how an integer type pointer points to the first element of a single array of integers) so I can do this

ptr[0][0][2].number = 8;

Obviously, this doesn't work, but it should give you an idea of what I'm trying to do. Any suggestions?


Maybe something like this? (not tested)

struct example {
int number;
};

typedef example[2][3][4] mymultarray;

mymultarray demo;
mymultarray *ptr;

ptr = &demo;
(*ptr)[0][0][2].number = 8;
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Mon May 15, 2006 9:54 pm    Post subject: Reply with quote

Quote:

Drakonite wrote:
struct example ***ptr; // ?

I don't think that would work. The compiler wouldn't know the size of each dimension.

It'll work fine - it's a pointer to the first element of the arrays. But whether it's any use...depends what the OP is trying to do. Often it can be easier to created a 1d array and perform the indexing oneself.

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
PlayfulPuppy



Joined: 05 May 2006
Posts: 22

PostPosted: Mon May 15, 2006 10:35 pm    Post subject: Reply with quote

moonlight wrote:
Drakonite wrote:
struct example ***ptr; // ?


I don't think that would work. The compiler wouldn't know the size of each dimension.


He's using a pointer to an array. The compiler wouldn't know the dimensions even if it was a 1D array.
Back to top
View user's profile Send private message
Drakonite
Site Admin


Joined: 17 Jan 2004
Posts: 989

PostPosted: Mon May 15, 2006 11:10 pm    Post subject: Reply with quote

PlayfulPuppy wrote:
moonlight wrote:
Drakonite wrote:
struct example ***ptr; // ?


I don't think that would work. The compiler wouldn't know the size of each dimension.


He's using a pointer to an array. The compiler wouldn't know the dimensions even if it was a 1D array.

The compiler doesn't care -- C doesn't check array boundries.

It would break pointers to multidementional arrays, but thats just one of the reasons why pointers to multidementional arrays (and typically multidementional arrays in general) are evil.

To get what the OP was really trying to do, I think there is some crap somewhere along the lines of 'struct example *(ptr[3][4]);' but tbh I avoid evil with multidementional arrays like this whenever possible, so I don't for sure if that is right.
_________________
Shoot Pixels Not People!
Makeshift Development
Back to top
View user's profile Send private message Visit poster's website
PlayfulPuppy



Joined: 05 May 2006
Posts: 22

PostPosted: Mon May 15, 2006 11:25 pm    Post subject: Reply with quote

I can't for the life of me work out why someone would want to do that in the first place, though.

If you want multiple elements in a hierachy, and you know what the dimensions are going to be, use a struct or a class to do it. It'll make the code more readable.

What are you using this in aid of?
Back to top
View user's profile Send private message
NeoXeno



Joined: 20 Oct 2005
Posts: 10

PostPosted: Tue May 16, 2006 5:05 am    Post subject: Reply with quote

Thanks everyone. Honestly, I just wanted to know if something like this was possible but judging from some of the comments here it probably isn't worth doing anyway. I think I'll just stick with what I had going.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group