[A89] Re: Pointers and Matricies


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

[A89] Re: Pointers and Matricies




> >If I have a matrix such as thus:
> >
> >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}};
> >
> >and I also have a pointer, thus:
> >
> >char * pmatrix1 = &matrix1;
> >
> >How would I go about referencing a single element in the matrix using ONLY
> >the pointer?
> >I tried doing this:
> >
> >pmatrix[1][1]=1;
> >
> >But that doesn't work.  Can someone help me out?  Much appreciation!
> >
> >CalenWakefield
> >
>
> I would write:
>
> 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;
>
> and then access the stuff with like
>
> pmatrix[1][1] = 1;
>
> Since the array is twodimensional you need two *, one for each [].
> it is a pointer to a pointer.
> putting a [] behind a pointer or array (wich is really the same thing,
> except you cant change the address of an array)
> like this pointer[5] is the same thing as writeing *(pointer+5)
>
> ///Olle

My C knowledge may be a little rusty, but how does pmatrix know the dimensions
of matrix?

--
 / Niklas Brunlid
Check out Prosit for the TI-89 / TI-92+ at http://prosit.ticalc.org
Random PQF v5.1 Quote follows:

- "Very clever idea, though."
- "What is?"
- "Asking the questions when people arrive. If anyone was coming here to
   do some subversive overthrowing, everyone'd be down on him like a pound of
   bricks as soon as he answered 'Yes'."
- "It's a sneaky trick, isn't it," said Angalo, in an admiring tone of
   voice.
        -- The nomes encounter American customs regulations
           (Terry Pratchett, Wings)







Follow-Ups: References: