How to manipulate struct pointer

This is a discussion on How to manipulate struct pointer within the C Programming forums, part of the General Programming Boards category; Hello, Can anyone please help to show me how to manipulate a struct pointer like this: Code: struct Point { ...

  1. #1
    C-Dumbie
    Guest

    How to manipulate struct pointer

    Hello,
    Can anyone please help to show me how to manipulate a struct
    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. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Why are you messing with a 3D pointer when only have two points in your struct???


    Mr. C.

  3. #3
    C-Dumbie
    Guest
    I miss one more coordinate z, it suppose to be:

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

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

    C-Dumbie

    Note: please post reply your solution if you could help, but dont with unhelpful anwers. We will waste time and space.

    Muncho Gracias!

  4. #4
    C-Dumbie
    Guest
    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. #5
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    >>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. #7
    C-Dumbie
    Guest
    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.
    Instead of defining as:
    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. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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.
    Instead of defining as:
    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.
    hello, internet!

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  10. #10
    C-Dumbie
    Guest
    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. #11
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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...
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  12. #12
    C-Dumbie
    Guest

    Talking

    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. #13
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    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
    hello, internet!

  14. #14
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    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;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  15. #15
    C-Dumbie
    Guest

    Cool

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 10:45 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. "dereferencing pointer to incomplete type"
    By incognito54 in forum C Programming
    Replies: 2
    Last Post: 11-01-2005, 08:50 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21