[A89] Re: Pointers and Matricies


[Prev][Next][Index][Thread]

[A89] Re: Pointers and Matricies




> char matrix1[5][5]=
> {{0,0,0,0,0}{0,0,0,0,0}{0,0,0,0,0}{0,0,0,0,0}{0,0,0,0,0}};
>
> char * pmatrix1 = &matrix1;

The above statement is wrong.  First of all, matrix1 is _already_ a pointer
to the beginning of the matrix, and thus you don't need the address-of
operator (the ampersand), although technically some compilers will accept
(and ignore) it.  Second, and more importantly, that matrix is actually of
type pointer-to-pointer-to-char.  What you've actually got in memory is the
pointer to one array which contains pointers to 5 other arrays, and each of
those arrays contains the characters.  In other words, the correct statement
would be:

char ** pmatrix = matrix1;

> pmatrix[1][1]=1;
>
> But that doesn't work.  Can someone help me out?  Much appreciation!

If pmatrix is defined correctly, that statement is fine.  Note that you
could also use:

*(*(pmatrix+1)+1) = 1;

Although in this instance, the simpler notation is obviously much more
convenient.  But if you don't understand why the second statement is valid,
then I highly recommend that you read up some more on how arrays are stored
in memory,

> Oh wait.....that assignment statement for the pointer should be:
>
> char * pmatrix1 = &matrix1[0][0];

You were more correct the first time around =)  Technically, this will work
as follows on when executed:

* Take matrix1, the pointer to the main array (which contains type char*)
* Take item 0 of that array, which is a pointer to the first sub-array
* Take item 0 of that sub-array, which is a null byte
* Take the address of that item, which is the same as the pointer to the
first sub-array

The problem now is twofold:

1.) The statement you've written is syntactically valid, but absolutely not
what you wanted.  What we have is not the address of the entire matrix, but
the address of the _first row_ of the matrix.  The separate rows are not
guaranteed to be stored contiguously in memory - after the first row, you
could have garbage.

2.) pmatrix1 is of type char*, but if we're referencing it as a
two-dimensional array, it must of type char**.  This is a side-effect of the
first problem.

The trick here is to realize that matrix1 already is a pointer to the
matrix, and you don't need to find the first item and take its address -
instead, use it as it is.  So the right side of the statment should simply
be "= matrix1;" -- if we try to get the first value and then find its
address, we'll almost certainly wind up somewhere other than we wanted if
it's a multi-dimensional array.

The other thing you must realize is that, once again, we have a
two-dimensional array, and thus variables representing it must be
pointers-to-pointers (and add another "pointers-to-" for every additional
dimension).  So our left side should be "char ** pmatrix".  Putting them
together, we arrive at:

char ** pmatrix = matrix1;

That should hopefully be what you're looking for.

    -Scott