# Thread: How to manipulate struct pointer

1. ## How to manipulate struct pointer

Hello,
pointer like this:
Code:
```  struct Point { int xcord;  int ycord};

struct Point  ***PointPtr; /*a point in 3D */

**(PointPtr->xcord) = 10;  /* is this right access? */```
Thanx!
C-dumbie

2. Why are you messing with a 3D pointer when only have two points in your struct???

Mr. C.

3. I miss one more coordinate z, it suppose to be:

struct Point { int xcord;
int ycord;
int zcord;
} ;

/* or */
struct Point ***pointPtr;

C-Dumbie

Muncho Gracias!

4. I have been expecting for your help on this question, but no one ring a bell. Where our
C experts on this board go? Anybody know?

C-Dumbie

5. Hey, I'm here!

Why use a triple pointer when you can just use a single one?

Code:
```#include <stdio.h>
#include <stdlib.h>

typedef struct
{
int xcord;
int ycord;
int zcord;
}POINT;

int main()
{
POINT *pointPtr = malloc(sizeof(POINT));
pointPtr->xcord = 1;
pointPtr->ycord = 2;
pointPtr->zcord = 3;
printf("My coordinate (%d,%d,%d)\n", pointPtr->xcord, pointPtr->ycord, pointPtr->zcord);
free(pointPtr);
return 0;
}```

6. >>struct Point ***PointPtr; /*a point in 3D */

You seem a little confused.....here you have a pointer to a pointer to a pointer to a stuct (phew!)...nothing 3d about it!

Now the question is what are you trying to do? If you are trying to create 3 of these structures, then you need to malloc(sizeof(struct Point)*3);

Then address each struct as an array IE

Code:
```struct Point {
int xcord;
int ycord;
int zcord;
} ;

int main (int argc,char** argv)
{
struct Point *PointPtr =
malloc(sizeof(struct Point)*3);//create 3 structs

PointPtr[2].zcord = 10;//assign z of 3rd struct to 10

free(PointPtr);

return 0;
}```
:: Edit Damn you CShot and you laser quick typing ::

7. Thanx for you guys help! and by the way congratulation to Fordy for winning the election!

The problem for the above question is I have
tried to create 3D array of a struct POINT which contain 3 coordinates for 3D vector space.
The program I work on is a 3D game.
struct POINT Arr3D[4][5][6];
I define as: struct POINT ***Arr3D; for
code efficience purpose.(using pointer is better)
but I really dont know how to manipulate it.
C-Dumbie

8. Originally posted by C-Dumbie
Thanx for you guys help! and by the way congratulation to Fordy for winning the election!

The problem for the above question is I have
tried to create 3D array of a struct POINT which contain 3 coordinates for 3D vector space.
The program I work on is a 3D game.
struct POINT Arr3D[4][5][6];
I define as: struct POINT ***Arr3D; for
code efficience purpose.(using pointer is better)
but I really dont know how to manipulate it.
C-Dumbie
i'm not sure if you quite understand,

struct POINT Arr3D[4][5][6];

makes 120 different (x,y,z) sets.

9. Perhaps you should clarify exactly what you're trying to do.

>> struct POINT Arr3D[4][5][6];
If this is your 3d vector space, then each point in that space has it's own 3d point based on how you've defined it.

Are you trying to do something like this?
int array[4][5][6]; // 1 if there's a point there, 0 if there isn't

10. Originally posted by Cshot
Perhaps you should clarify exactly what you're trying to do.

>> struct POINT Arr3D[4][5][6];
If this is your 3d vector space, then each point in that space has it's own 3d point based on how you've defined it.

Are you trying to do something like this?
int array[4][5][6]; // 1 if there's a point there, 0 if there isn't
Here is just a example code segment to clarify my intended purpose to use 3D array using pointer for dynamic allocation and
code efficiency:
Code:
```typedef enum {MARS, MERCURY, JUPITER, MOON} PlanetAlien;
typedef enum {LEFT, RIGHT, MIDDLE} CrossHair;
typedef enum {SHOOT, LOAD, MOVE, PAUSE} GunControl;

typedef struct {int x , y , z; } POINT;

POINT Arr3D[4][3][4];

/* Shoot an alien from Mars planet at Point (10, 20, 30) in uninverse space */
Arr3D[MARS][RIGHT][SHOOT].x = 10;
Arr3D[MARS][RIGHT][SHOOT].y = 20;
Arr3D[MARS][RIGHT][SHOOT].z = 30;

/*.............................................*/

But I dont quite understand clearly in using, allocate and manipulate with POINT ***Array3D.

Hope you guys get an idea what I am trying to do.

Regards,
C-Dumbie```

11. This should get you in the right direction:

Code:
```#include <stdio.h>

typedef enum {MARS, MERCURY, JUPITER, MOON} PlanetAlien;
typedef enum {LEFT, RIGHT, MIDDLE} CrossHair;
typedef enum {SHOOT, LOAD, MOVE, PAUSE} GunControl;
typedef struct {int x , y , z; } POINT;

POINT Arr3D[4][3][4];
void doStuff(POINT [][3][4], int, int, int);

int main()
{
/* Shoot an alien from Mars planet at Point (10, 20, 30) in uninverse space */
Arr3D[MARS][RIGHT][SHOOT].x = 10;
Arr3D[MARS][RIGHT][SHOOT].y = 20;
Arr3D[MARS][RIGHT][SHOOT].z = 30;
doStuff(Arr3D, MARS, RIGHT, SHOOT);
return 0;
}

void doStuff(POINT d[][3][4], int a, int b, int c)
{
d[a][b][c].x += 10;
d[a][b][c].y += 10;
d[a][b][c].z += 10;
printf("(%d,%d,%d)\n", d[a][b][c].x, d[a][b][c].y, d[a][b][c].z);
return;
}```
Like what Fordy said:
>>struct Point ***PointPtr; /*a point in 3D */

You seem a little confused.....here you have a pointer to a pointer to a pointer to a stuct (phew!)...nothing 3d about it!
Think about what ***PointPtr actually is...

12. Forget about it, Cshot. Thanx anyway!

My intended purpose is trying to create a dynamical 3d array instead of 3D static array.
using the array and pointer concept in C:

TYPE *ArrPtr1D; /*This can be a 1D array,same as TYPE Arr[] */
TYPE **ArrPtr2D; /* This can be a 2D array, same as TYPE Arr[][] */
TYPE ***Arr; /* This can be a 3D array, same as TYPE Arr[][][]*/
where TYPE could be an int, float or struct...

Creating static 3D array is just simple as:
struct POINT Arr3D[2][2][2]; but creating
dynamical 3D array, struct POINT ***Point3DPtr with malloc() seems like truggling for me.
I wonder if you guys C expert ever creating an
dynamical 3D array of struct in C using malloc()??? Typically with int***p = malloc(..)
C-Dumbie

13. if you're really sure this is what you want:

Code:
```//
int i, j;
int xsize, ysize, zsize;
foobar_t ***points;
// insert code to initalize xsize,ysize,zsize here

// now we dynamically allocate
points = malloc (sizeof (foobar_t **) * xsize);
for (i = 0; i < xsize; i++)
{
points[i] = malloc (sizeof (foobar_t *) * ysize);
for (j = 0; j < ysize; j++)
{
points[i][j] = malloc (sizeof (foobar_t) * zsize);
}
}
// access each point with points[xpos][ypos][zpos]
//flip the whole thing around to free the stuff```

14. Of course we have at one point or another:

Code:
```#include <stdio.h>
#include <stdlib.h>

typedef enum {MARS, MERCURY, JUPITER, MOON} PlanetAlien;
typedef enum {LEFT, RIGHT, MIDDLE} CrossHair;
typedef enum {SHOOT, LOAD, MOVE, PAUSE} GunControl;
typedef struct {int x , y , z; } POINT;

POINT ***Arr3D;   // we want the equivalent of Arr3D[4][3][4];

int main()
{
int i, j;

// allocate memory
Arr3D = malloc(sizeof(POINT**) * 4);
for(i = 0; i < 4; i++)
{
Arr3D[i] = malloc(sizeof(POINT*) * 3);
for(j = 0; j < 3; j++)
Arr3D[i][j] = malloc(sizeof(POINT) * 4);
}

Arr3D[MARS][RIGHT][SHOOT].x = 10;
Arr3D[MARS][RIGHT][SHOOT].y = 20;
Arr3D[MARS][RIGHT][SHOOT].z = 30;

printf("(%d,%d,%d)\n",
Arr3D[MARS][RIGHT][SHOOT].x,
Arr3D[MARS][RIGHT][SHOOT].y,
Arr3D[MARS][RIGHT][SHOOT].z);

// free memory
for(i = 0; i < 4; i++)
{
for(j = 0; j < 3; j++)
free(Arr3D[i][j]);
free(Arr3D[i]);
}

return 0;
}```

15. There you go!
Finally, That's what I looking for!
Thanx guys.
C-Dumbie