How to create a pointer to multidimensional array of structs
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?
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?
Multidimentional variables are ugly, and as soon as you bring pointers into it you are just causing trouble.
Shoot Pixels Not People!
Makeshift Development
Makeshift Development
Re: How to create a pointer to multidimensional array of str
Maybe something like this? (not tested)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?
struct example {
int number;
};
typedef example[2][3][4] mymultarray;
mymultarray demo;
mymultarray *ptr;
ptr = &demo;
(*ptr)[0][0][2].number = 8;
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.Drakonite wrote:
struct example ***ptr; // ?
I don't think that would work. The compiler wouldn't know the size of each dimension.
Jim
-
- Posts: 22
- Joined: Fri May 05, 2006 12:04 am
The compiler doesn't care -- C doesn't check array boundries.PlayfulPuppy wrote:He's using a pointer to an array. The compiler wouldn't know the dimensions even if it was a 1D array.moonlight wrote:I don't think that would work. The compiler wouldn't know the size of each dimension.Drakonite wrote:struct example ***ptr; // ?
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
Makeshift Development
-
- Posts: 22
- Joined: Fri May 05, 2006 12:04 am