| View previous topic :: View next topic |
| Author |
Message |
NeoXeno
Joined: 20 Oct 2005 Posts: 10
|
Posted: Mon May 15, 2006 5:54 pm Post subject: How to create a pointer to multidimensional array of structs |
|
|
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 |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
Posted: Mon May 15, 2006 6:52 pm Post subject: |
|
|
struct example ***ptr; // ? _________________ Shoot Pixels Not People!
Makeshift Development |
|
| Back to top |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Mon May 15, 2006 7:12 pm Post subject: |
|
|
| 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 |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
Posted: Mon May 15, 2006 7:18 pm Post subject: |
|
|
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 |
|
 |
moonlight
Joined: 26 Oct 2005 Posts: 567
|
Posted: Mon May 15, 2006 7:59 pm Post subject: Re: How to create a pointer to multidimensional array of str |
|
|
| 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 |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Mon May 15, 2006 9:54 pm Post subject: |
|
|
| 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 |
|
 |
PlayfulPuppy
Joined: 05 May 2006 Posts: 22
|
Posted: Mon May 15, 2006 10:35 pm Post subject: |
|
|
| 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 |
|
 |
Drakonite Site Admin

Joined: 17 Jan 2004 Posts: 989
|
Posted: Mon May 15, 2006 11:10 pm Post subject: |
|
|
| 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 |
|
 |
PlayfulPuppy
Joined: 05 May 2006 Posts: 22
|
Posted: Mon May 15, 2006 11:25 pm Post subject: |
|
|
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 |
|
 |
NeoXeno
Joined: 20 Oct 2005 Posts: 10
|
Posted: Tue May 16, 2006 5:05 am Post subject: |
|
|
| 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 |
|
 |
|