pointers to pointers to arrays 
Author Message
 pointers to pointers to arrays

At first I want to tell you: I am just learning c!
(so don't be angry, if my question is stupid)
All right:
Image you have got a (n,m) matrix in the main program.
Now you want to do something with it in a routine
(e.g. add 10 to the element [0][0]) and go back to
the main program. And now I want the computer to remember,
what he did in the routine (the way I do it with these **s
he always forgets it coming back to the main program).
That's the way I tried:

void test(double** matrix);

int main ()
{
  double **m;
  ...

  test(m);
  ...

Quote:
}

void test(double** matrix)
{
matrix[0][0] += 10;
return

Quote:
}

someone told me, I have to use ***. Is this right
and how have I to do this???

Andrea



Thu, 29 Jan 1998 03:00:00 GMT  
 pointers to pointers to arrays

[snip]
: Image you have got a (n,m) matrix in the main program.
: Now you want to do something with it in a routine
: (e.g. add 10 to the element [0][0]) and go back to
: the main program. And now I want the computer to remember,
[snip]
: someone told me, I have to use ***. Is this right
: and how have I to do this???
[snip]

Hi, Andrea!

The following program does it:

#include <stdio.h>
#include <stdlib.h>

void test(double** matrix);

int main ()
{
  double** m = (double**) malloc(2 * sizeof (double*));
  m[0] = (double*) malloc(2 * sizeof (double));
  m[1] = (double*) malloc(2 * sizeof (double));
  m[0][0] = m[0][1] = m[1][0] = m[1][1] = 0.0;

  printf("%lf\n", m[0][0]);
  test(m);
  printf("%lf\n", m[0][0]);

  free(m[1]);
  free(m[0]);
  free(m);

  return 1;

Quote:
}

void test(double** matrix)
{
    matrix[0][0] += 10;

    return;

Quote:
}

So, you don't have to use ***. If you wanted to e.g. move the matrix
to another memory location in the test function, then you should use
***.

Regards,
Jukka

----------------------------------------------------------------------
Jukka Liukkonen                           Geological Survey of Finland
Senior ADP Designer                       Betonimiehenkuja 4

Fax: 358-0-462205                         FINLAND
WWW: http://www.gsf.fi/~4jukka
----------------------------------------------------------------------



Fri, 30 Jan 1998 03:00:00 GMT  
 pointers to pointers to arrays

Quote:

>void test(double** matrix);

>int main ()
>{
>  double **m;
>  ...

>  test(m);
>  ...
>}

>void test(double** matrix)
>{
>matrix[0][0] += 10;
>return
>}

>someone told me, I have to use ***. Is this right
>and how have I to do this???

>Andrea

There are a number of ways to do what you want to do, and they depend mostly
on how and where the storage for your two dimensional array is allocated.
Since you are declaring a pointer-to-pointer-two-double, you don't strictly
have a two dimensional array. What you have (or will have, as soon as you
allocate the storage) is an array of pointers, each of which points to (the
first element of) an array of doubles.

Since your elipses ("...") appear in main only, I will assume you intended for
storage allocation to occur in main, rather than in test, which already looks
like you intended that to be its complete definition. You don't need triple
asterisks here (although there is a way you *can* make use of triple asterisks
if you wanted test to supply the storage, but even then it's an unnecessarily
complicated way to do it, IMO)

One way to make the main program complete is to allocate the storage like this:

#include <stdlib.h>
int main()
{
  double **m;
  int rows = 4;
  int cols = 5;
  int i;

  /* allocate the array of rows -- an array of pointers */
  m = malloc ( rows * sizeof(double *));

  /* Allocate the doubles in each row */
  for (i=0; i<rows; i++) {
    m[i] = malloc( cols * sizeof(double));
  }

  /* Now call test as before */
  test(m);
  return 0; /* Remember,  you can't just "return" from an int function */

Quote:
}

This main program will work with your test routine as expected. The
comp.lang.c FAQ list discusses many similar questions about multidimensional
arrays and passing them to functions. I highly recommend reading it,
especially section 2.

-Chris

--

Chris Volpe                             Phone: (518) 387-7766 (Dial Comm 8*833
GE Corporate R&D                    Fax:   (518) 387-6560



Wed, 04 Feb 1998 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Pointer of Pointers was Pointer of arrays...

2. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

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

4. Array of pointers, pointer to array...

5. array pointer/pointer array

6. arrays pointers array of pointers

7. Pointer to array of pointers

8. Pointer to an array of pointers to functions

9. limit on pointer to array of pointers?

10. Pointer to Array of Pointers to Strings.

11. pointer to array vs pointer

12. Pointer to pointer of an array

 

 
Powered by phpBB® Forum Software