[A89] Re: Pointers and Matricies


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

[A89] Re: Pointers and Matricies




First of all, please refer to my last reply to this list for the _correct_
solution to the problem.

> First, define a type for your matrix, for example:
>
> typedef char my_type[rows][cols];
>
> Yeah, I know.  C syntax sucks.

Well, if you used the language in the way it was meant to be used and
weren't trying to use this typedef workaround, it wouldn't be bad.  And
although I agree that the typedef syntax is ugly, the real problem is that C
syntax doesn't seem logical if you don't understand how it works internally.
When you realize _why_ each feature of the language works like it does (and
I've read a couple books and essays on this), then, IMHO, it seems like the
most sensible syntax of all, with a few shortcomings (like the type of var2
in "char* var, var2;" is not a pointer. . .)

> Then define a matrix of that type and initialize it, like:
>
> my_type matrix1 = {...};

So your matrix is now of type char**, with appropriate dimensions.

> Then define a variable holding a pointer to that matrix, like:
>
> my_type *pmatrix = &matrix1;

Two problems with that statement.  First of all, you shouldn't need to take
the address-of of an array name, which is already a pointer equal to the
address of the array.  Second, what's being returned is not of type "pointer
to my_type," but of type "my_type" itself -- since the address of an array
is the exact same thing as the array name by itself.  In other words,
matrix1 is already a pointer to the array of my_type (ugh, that's a bad
explanation =( ).  A more correct statement would be:

my_type pmatrix = matrix1;

Although I do believe that what you had above would work, I'm not sure,
because I've seen different compilers react to using the address-of operator
on arrays in different ways.  It's asking for trouble to think of anything
as a pointer to an array, when the array instead is a pointer to its own
contents.

> Then you can access the elements of the matrix that pmatrix points to by
> doing the following:
>
> my_value = (*pmatrix)[my_row][my_col];
> (*pmatrix)[my_row][my_col] = my_new_value;

Those are correct with your method (if it works, that is, and I believe a
followup said that it did), although if you use mine, there's no need to
dereference a pointer in order to access the array's contents (and there
certainly shouldn't be, because doing so is incredibly wasteful when there
was no purpose to trying to get a pointer to the array instead of the
address of the array itself.  If you use my

> If this doesn't work, it's probably my fault, so don't hesitate...

Technically, it is _a_ working solution to the problem.  But it's not the
real correct solution, and it's a poor way to try and make up for a lack of
understanding of the language and how it works.

If this sounds harsh, I don't mean any offense to Sebastian.  I just want to
keep a less-experienced C programmer on the right track, since I've seen
several people who try to use solutions like these in computer science
classes, and in the end they have many more problems that result from their
misunderstanding of the basic concepts.  Not to mention that I had to defend
C's syntax against those monstrous heaps of parentheses, brackets, and
asterisks that he had working for him.

    -Scott