How to return pointers to matrices 
Author Message
 How to return pointers to matrices

        Ok, I am wondering if there is a way that you can pass
pointers to matrices.  Ie if I defined test1 as follws:
        char test1[20][20];
Is it possible to have a funtion that would return a double pointer to
it?  And if so, how would I have to do it?  I have tried
        char ** returnTest() {
                return test1;
        }
But all that I get is errors on compilation (don't remember them off
the top of my head).  Anyone have any suggestions?
Thanks,
--




Mon, 21 Aug 1995 08:49:26 GMT  
 How to return pointers to matrices

Quote:


>>You should do like this:
>>        char (*)[20] returnTest() {
>>                return test1;
>>        }

>I don't think this will do what Scott really wants.  The 20 pointers to
>the data in the array test1 don't really exist in memory.  You need to
>allocate the array of pointers, and then assign each row of test to
>each pointer.

Depends what Scott wants; I didn't understand his post well enough
to give him a solution. If he wants something like:

   char (*)[20] returnTest ()
   {
      static char test1[20][20];

      return test1;
   }

then Ken's solution is perfect.

I think you are misparsing the return type of Ken's function. It
is not an array of 20 pointers. It is a pointer to type <array 20 of char>.
And this is exactly the type that arrays of type char[20][20]
are converted to when the name of the array appears in a value
context.

--
Dave Eisen                               "To succeed in the world, it is not

Sequoia Peripherals: (415) 967-5644       be well-mannered." --- Voltaire
Home:                (415) 321-5154  



Wed, 23 Aug 1995 11:28:29 GMT  
 How to return pointers to matrices

Definitely a bad week folks. Sorry for the misinformation.

Quote:


>>>You should do like this:
>>>    char (*)[20] returnTest() {
>>>            return test1;
>>>    }

Which I carelessly agreed with. The correct syntax for a function
returning a pointer to type <array 20 of char> is:

   char (*returnTest ()) [20]
   {
      static char test1[20][20];

      return test1;
   }

To construct this correctly:

returnTest is a function:

   returnTest ()

returnTest is a function returning a pointer:

   *returnTest ()

returnTest is a function returning a pointer to type <array 20 of char>
so (*returnTest()) *is* an 20 element character array and should
be written as such:

   char (*returnTest ())[20]
   {
   }

Thanks to the several posters who caught me this time.

--
Dave Eisen                               "To succeed in the world, it is not

Sequoia Peripherals: (415) 967-5644       be well-mannered." --- Voltaire
Home:                (415) 321-5154  



Thu, 24 Aug 1995 01:19:50 GMT  
 How to return pointers to matrices

Quote:



>>>    Ok, I am wondering if there is a way that you can pass
>>>pointers to matrices.  Ie if I defined test1 as follws:
>>>    char test1[20][20];
>>>Is it possible to have a funtion that would return a double pointer to
>>>it?  And if so, how would I have to do it?  I have tried
>>>    char ** returnTest() {
>>>            return test1;
>>>    }
>>>But all that I get is errors on compilation (don't remember them off
>>>the top of my head).  Anyone have any suggestions?
>>>Thanks,
>>>--


>>You should do like this:
>>        char (*)[20] returnTest() {
>>                return test1;
>>        }

>I don't think this will do what Scott really wants.  The 20 pointers to
>the data in the array test1 don't really exist in memory.  You need to
>allocate the array of pointers, and then assign each row of test to
>each pointer.

What a bogus am I! I made a ridiculous mistake in my previous article.
I had to do like this:
        char (*returnTest())[20]
        {
          return test1;
        }


Happy hacking!

Ken Nakata

Quote:

>--
>Glen Niebur     | If i die today, i'll be the happy phantom
>Mayo Clinic     | and i'll go chasing the nuns out in the yard.
>Biomechanics Lab|


--

* I would apologize if there are incorrect, rude, and/or impolite expressions
in this mail or post though they are not inteded.  Please consider that English
is a second language for me.  Thank you. *


Sun, 27 Aug 1995 03:56:21 GMT  
 How to return pointers to matrices
cdecl> explain char (*returnTest())[20]
declare returnTest as function returning pointer to array 20 of char
cdecl> explain (*)[20] returnTest()
syntax error
cdecl> declare returnTest as function returning pointer to array 20 of
char
char (*returnTest())[20]


Sun, 27 Aug 1995 16:22:14 GMT  
 How to return pointers to matrices

Quote:



>>>>        Ok, I am wondering if there is a way that you can pass
>>>>pointers to matrices.  Ie if I defined test1 as follws:
>>>>        char test1[20][20];
>>>>Is it possible to have a funtion that would return a double pointer to
>>>>it?  And if so, how would I have to do it?  I have tried
>>>>        char ** returnTest() {
>>>>                return test1;
>>>>        }
>>>>But all that I get is errors on compilation (don't remember them off
>>>>the top of my head).  Anyone have any suggestions?
>>>>Thanks,
>>>>--

>>I don't think this will do what Scott really wants.  The 20 pointers to
>>the data in the array test1 don't really exist in memory.  You need to
>>allocate the array of pointers, and then assign each row of test to
>>each pointer.

>What a bogus am I! I made a ridiculous mistake in my previous article.
>I had to do like this:
>    char (*returnTest())[20]
>    {
>      return test1;
>    }

The above is pretty typical of the the threads running through here
about multi-dimensional arrays.  Everytime I see one, I think about
how I've written hundreds of thousands of lines of C without ever
getting as confused as so many folks here seem to be.  My "work-around"
is quite straight forward and generally more general :-).

I _never_ use them.

Confronted with multi-dimensional data, I decompose it into single-
dimensioned components.  For example:

typedef struct  element_        /* an element consists of an x (and a y) */
{
        int             x;
        int             y;      /* maybe? */

Quote:
}

element_;

typedef struct  column_         /* a column consists of 10 rows of elements */
{
        element_        row[10];

Quote:
}

column_;

typedef struct  matrix_         /* a matrix consists of 20 columns */
{
        column_         col[20];

Quote:
}

matrix_;

#define lower(a)        ((a), 0)
#define upper(a)        ((sizeof(a) / sizeof(*(a))) - 1)

matrix_ *
matrix_init(matrix_ *m)                 /* initialize matrix */

{
    int     c, r;

    for (c = lower(m->col); c <= upper(m->col); c += 1)
    {
        for (r = lower(m->col[c].row); r <= upper(m->col[c].row); r += 1)
        {
            m->col[c].row[r].x = 0;
        }
    }
    return(m);

Quote:
}

Notes:

0.      Those of you that winced upon seeing the 'lower' macro
        can manually substitute '0'.  I rarely use it myself ;-).

1.      Of course, 'col' and 'row' should usually be something
        more meaningful.  The example will read better if you
        plug in your own names.

2.      The definition of 'element_' could have been

                typedef int     element_;

        (or omitted completely) if you are _dead_ certain that you
        won't want to make the element more complex in the future.

3.      The _named_ structures in the typedefs are not necessary,
        but I prefer things to be done the same way all the time
        and often you need the names to make self-referencing
        structures.

4.      The ((a), 0) in the 'lower' macro avoids complaints about
        unused arguments in macros.

Advantages:

1.      References to the data structure are "self-documenting".

2.      An (initially) simple data structure can be extended
        to be much more complex without visiting existing
        references.

3.      You are encouraged to create the typedef's up-front so that
        function return type declarations, for example, are clean
        and, again, self-documenting.

4.      You'll never have to wonder "do I want '*' or '**'?".

Disadvantages:

        I can't think of any.  There should be no size or
        run-time penalty unless you take advantage of multi-
        membered structures.  And don't tell me that

                m[c][r] = 0;

        is more "concise"!

It being legal doesn't _necessarily_ make it good for you...

bob.                                                  |I only smile when I lie,
``The truth angers those that it fails to convince.'' |And I'll tell you why...
Were these more than just my opinions, they would have cost a bit more.



Mon, 28 Aug 1995 07:08:18 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Pointers: return of pointer to array of pointers to main

3. Return Structure or return Pointer??

4. Can function returning pointer return negative integers?

5. Returning Pointer To Pointer From Function

6. Question about signal()/pointers to functions that return pointers to functions

7. add and multiply matrices with pointer Arithmetic help.

8. pointer to matrix syntax

9. Pointers and matrices

10. Q: Matrixes and pointers

11. Pointers & matrices: Twist on the original question

12. problem with malloc and pointer matrixes

 

 
Powered by phpBB® Forum Software