sven@cs.widener.edu (Sven Heinicke) (03/24/91)
I got a little double integer pointer in a structure problem while in a
subprogram problem.  I think I have got everyting but one line down but 
I am not 100% sure about the malloc() line either.  Here is the extract
of the code I am working on:
.
.
.
struct imatrix {
  char **board;
  int size;
};
typedef struct imatrix iMATRIX;
main()
{
  int l;
  iMATRIX playing;
.
.
.
  /*
   * here are the three malloc() lines I am
   * not sure work right but I think do.
   * playing.size is picked bye the user.
   */
  playing.board = (int**)malloc(sizeof(int *) * playing.size * playing.size); 
  for(l = 0;l < playing.size * playing.size;l++)
    *(playing.board) = (int*)malloc(sizeof(int));
  reset(&playing);
.
.
.
}
reset(matrix)
     iMATRIX *matrix;
{
  int x,y;
  for(x = 0;x < matrix->size;x++)
    for(y = 0;y < matrix->size;y++)
      (*(matrix->board+x)+y) = x * matrix->size + y + 1;
  /*
   * This in the line that I got confused about, 
   * it don't compile.  In gnu c it gets the error:
   *           invalid lvalue in assigment
   *
   * could somebody tell me what line to use?
   *
   */
}
-- 
sven@cs.widener.edu                                  Widener CS system manager
Sven Mike Heinicke                                          and Student
(pssmheinicke@cyber.widener.edu (if you must))rjohnson@shell.com (Roy Johnson) (03/27/91)
In article <1991Mar23.213414.12572@cs.widener.edu> sven@cs.widener.edu (Sven Heinicke) writes: > struct imatrix { > char **board; > int size; > }; > typedef struct imatrix iMATRIX; > main() > { > int l; > iMATRIX playing; > . > . > . > playing.board = (int**)malloc(sizeof(int *) * playing.size * playing.size); I think what you want here is playing.board = (int **)malloc(sizeof(int *) * playing.size); which gives you one dimension of the board. Then to get the other dimension you do > for(l = 0;l < playing.size * playing.size;l++) > *(playing.board) = (int*)malloc(sizeof(int)); which I gather is to allocate the other dimension of the board. What you are doing here is malloc-ing space, assigning the pointer, and then losing the pointer when you reassign the same thing again. This loop should be like: for (l = 0; l < playing.size; l++) playing.board[l] = (int *)malloc(sizeof(int) * playing.size); playing.board is then an array of arrays% of ints. However, note that you declared playing.board to be pointer to pointer to char. You will need to determine which you really want. > reset(matrix) iMATRIX *matrix; > { > int x,y; > for(x = 0;x < matrix->size;x++) > for(y = 0;y < matrix->size;y++) > (*(matrix->board+x)+y) = x * matrix->size + y + 1; > } For clarity, I recommend the (arguably slower) matrix->board[x][y] = x * matrix->size + y + 1; which should do the same thing. Should means I haven't tried it. 8^) If you want to keep your pointer-dereference notation, you want: *(*(matrix->board+x)+y) = x * matrix->size + y + 1; that is, one more level of indirection. Hope this helps. Hope it's all right. % I'm using the terms array and pointers rather freely, which I'm sure will inspire much "clarification". Conceptually, array of arrays is what you have. -- ======= !{sun,psuvax1,bcm,rice,decwrl,cs.utexas.edu}!shell!rjohnson ======= Feel free to correct me, but don't preface your correction with "BZZT!" Roy Johnson, Shell Development Company