Thread: 3d array of pointers to linked lists

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    3

    3d array of pointers to linked lists

    Hi, I am having some trouble getting a 3d array of pointers to link up with a linked list for each of the array elements.

    the 3d array of pointers is declared like this:

    Code:
    struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS];
    and there are linked lists declared like this:

    Code:
    struct particle {        /* structure for particles */
      double sw[3];        /* square well components */
      double hs[3];        /* hard sphere components */
      double u[3];        /* unit vector for rotations */
      struct particle *link;
    };
    I want to get the array pointers 'cell[x][y][z]' to point to the first observed particle in a function next to main, something like this:

    Code:
    void generate_list(){
    
      int i,j,k,l;
      /* determine the number of cells to decompose the simulation box in each of x, y and z  directions*/
      int(cells_x) = floor(boxX/cell_size);
      int(cells_y) = floor(boxY/cell_size);
      int(cells_z) = floor(boxZ/cell_size);
    
      /* initialise the array of pointers to NULL */
      for (j=0;j<cells_x;j++){
        for (k=0;k<cells_y;k++){
          for (l=0;l<cells_z;l++){
        cell[j][k][l] = NULL;
          }
        }
      }
      
      /* determine the cell size */
      cell_size_x = boxX/cells_x;
      cell_size_y = boxY/cells_y;
      cell_size_z = boxZ/cells_z;
    
      /* populate cells with pointers to the first observed particles in each cell */
      for (i=0;i<N;i++){
        /* find which cell particle i is in */
        i_cell_x = (int)(array[i].sw[0]/cell_size_x);
        i_cell_y = (int)(array[i].sw[1]/cell_size_y);
        i_cell_z = (int)(array[i].sw[2]/cell_size_z);
        /* check whether cell already points to a particle, if not: point to current particle, else edit the pointer of the occupying particle to the current particle */
        if (cell[i_cell_x][i_cell_y][i_cell_z] == NULL){
          cell[i_cell_x][i_cell_y][i_cell_z] = &(array[i]);
        } else {
          
        }
      }
    
    }
    I am getting a pointer type cast error when I compile "assignment from incompatible pointer type", can someone help please?
    Last edited by simulates; 08-08-2013 at 10:53 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int * cell[MAXCELLS][MAXCELLS][MAXCELLS];
    So why isn't this
    struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Quote Originally Posted by Salem View Post
    > int * cell[MAXCELLS][MAXCELLS][MAXCELLS];
    So why isn't this
    struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS];
    It is now.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    If you still have the error, how are you declaring your variable "array"? I don't seem to see its declaration in your function.

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    3
    Quote Originally Posted by MutantJohn View Post
    If you still have the error, how are you declaring your variable "array"? I don't seem to see its declaration in your function.
    Thanks for your reply MutantJohn, array[] is declared with variable length at runtime and filled up based on a parameter N (= number of particles).

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    MutantJohn was asking for the actual declaration, not a general description.

    Do you still have the problem? If so, and if you've changed anything, post your new code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    I've personally used something like
    Code:
    typedef struct particle particle_t;
    struct particle {
        struct particle *next;
        double x;
        double y;
        double z;
    };
    
    typedef struct {
        size_t xsize;
        size_t ysize;
        size_t zsize;
        double xmin;
        double ymin;
        double zmin;
        double xscale; /* xsize / (xmax - xmin), x = [xmin, xmax) */
        double yscale; /* ysize / (ymax - ymin), y = [ymin, ymax) */
        double zscale; /* zsize / (zmax - zmin), z = [zmin, zmax) */
        particle_t **list; /* list[x + xsize*(y + ysize*z)] */
    } volume_t;
    with the list in volume_t initialized to xsize*ysize*zsize NULL pointers. Adding a particle_t is very simple:
    Code:
    void add_particle(volume_t *const volume, particle_t *const particle)
    {
        const size_t x = (particle->x - volume->xmin) * volume->xscale;
        const size_t y = (particle->y - volume->ymin) * volume->yscale;
        const size_t z = (particle->z - volume->zmin) * volume->zscale;
        const size_t i = x + volume->xsize * (y + volume->ysize * z);
    
        particle->next = volume->list[i];
        volume->list[i] = particle;
    }
    which simply prepends the particle to the list that hangs off the list member of the volume_t.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of pointers to linked lists
    By junglist in forum C Programming
    Replies: 3
    Last Post: 05-06-2011, 02:17 PM
  2. Polynomials: Array of pointers to linked lists?
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 01:55 AM
  3. Help with pointers and linked lists
    By Christine in forum C Programming
    Replies: 3
    Last Post: 02-13-2011, 11:21 AM
  4. Help with linked lists/ pointers
    By anything25 in forum C Programming
    Replies: 14
    Last Post: 06-26-2009, 02:41 PM
  5. Linked Lists & Pointers
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 07:26 AM

Tags for this Thread