How to create a pointer to multidimensional array of structs

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
NeoXeno
Posts: 10
Joined: Thu Oct 20, 2005 5:35 am

How to create a pointer to multidimensional array of structs

Post by NeoXeno »

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?
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

struct example ***ptr; // ?
Shoot Pixels Not People!
Makeshift Development
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Drakonite wrote:struct example ***ptr; // ?
I don't think that would work. The compiler wouldn't know the size of each dimension.
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

Multidimentional variables are ugly, and as soon as you bring pointers into it you are just causing trouble.
Shoot Pixels Not People!
Makeshift Development
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Re: How to create a pointer to multidimensional array of str

Post by moonlight »

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;
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

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
PlayfulPuppy
Posts: 22
Joined: Fri May 05, 2006 12:04 am

Post by PlayfulPuppy »

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.
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

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
PlayfulPuppy
Posts: 22
Joined: Fri May 05, 2006 12:04 am

Post by PlayfulPuppy »

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?
NeoXeno
Posts: 10
Joined: Thu Oct 20, 2005 5:35 am

Post by NeoXeno »

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.
Post Reply