Trouble with pointers to arrays

dewils80
dewils80 used Ask the Experts™
on
Right...I need help because my head is spinning:

I have a program built in C++ on linux in a module fashion. I have a controlling module which simply calls the functions of other modules, and supplies data.

In one module (file_report_analyser) i have three arrays, all two dimensional (matched[50] [50], to_download[50] [50] and to_upload [50] [50]).

Now, i need to call this module, so that it does it's fnacy things to the arrays. Once it has run, I then need the control module to be able to retrieve values from these three arrays.
This is where i have got myself muddled.

I know that i need to shove pointers to the arrays around, but i can't sort out in my head what needs to go where.

Do i allocate memory to the arrays from the control module and pass the addresses to the analyser module,
OR
return the addresses of the arrays from the analyser module to the control module
OR
WHAT???


Please could someone explain the best method with some generic code, so i can see what happens. Please could you include the syntax for the explanation, including function declarations and pointer things.


Thanks in advance.

Dewils80
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
>>Do i allocate memory to the arrays from the control module and pass the addresses to the
>> analyser module,
>> OR
>> return the addresses of the arrays from the analyser module to the control module
>> OR
>> WHAT???

I would allocate the memory in the control module and pass the pointers to the other modules.

Commented:
I tried the following code and it works...

CODE:

#include<stdio.h>

void file_report_analyzer(int a[2][2], int b[2][2]);

void main() {
        int a[2][2]={1,2,3,4};
        int b[2][2]={6,7,8,9};

        printf("Before\n");
        printf("array1:%d\t%d\t%d\t%d\n", a[0][0], a[0][1], a[1][0], a[1][1]);
        printf("array2:%d\t%d\t%d\t%d\n", b[0][0], b[0][1], b[1][0], b[1][1]);
        file_report_analyzer(a, b);
        printf("After\n");
        printf("array1:%d\t%d\t%d\t%d\n", a[0][0], a[0][1], a[1][0], a[1][1]);
        printf("array2:%d\t%d\t%d\t%d\n", b[0][0], b[0][1], b[1][0], b[1][1]);
}

void file_report_analyzer(int a[2][2], int b[2][2]){
        a[0][0]=11;
        a[0][1]=12;
        a[1][0]=13;
        a[1][1]=14;
        b[0][0]=16;
        b[0][1]=17;
        b[1][0]=18;
        b[1][1]=19;
}


RESULTS:
Before
array1:1        2       3       4
array2:6        7       8       9

In file report analyzer function.
After
array1:11       12      13      14
array2:16       17      18      19

hth,

Devinder
Author of the Year 2009

Commented:
You could make a class or struct that contains all of the data:

struct AllTheData
{
    int matched    [50][50];
    int to_download[50][50];
    int to_upload  [50][50];
};

Then just allocate a copy of it -- in the controller -- and pass just its pointer to the analyser:

main()
{
    AllTheData rData;
    rData.matched[0][3]= 7;    
    rData.to_download[43][0]= 99;
    ...etc...
    file_report_analyser( &rData );
...

void file_report_analyser( AllTheData* prData )
{
   prData->matched[0][3]= prData->to_download[43][0];
   ... etc...
}

-- Dan
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Commented:
What I gather is you have a main function(file_report_analyser();) and that data is in them so it looks like this:

// Start
#define max_rows 50
#define max_cols 50

int main ()
{
  int
    matched [max_rows][max_cols],
    to_download [max_rows][max_cols],
    to_upload [max_rows][max_cols];

  void file_report_analyser():
 
  return 1;
} // main
// End

Of course I'm sure you have things in there to give the arrays values and such.  Ok, passing arrays.  What you need to remember is the name of an array is in fact a pointer.  Think of it like this.  If you did:

matched[0][0] = 1;

And then wanted to print that out, you have two options:

cout << matched[0][0] << endl;
// or
cout << *matched << endl;

The name of an array is a pointer to the first position of that array.  So, what if you wanted to access matched[0][1]?

cout << *(matched + 1) << endl;

This is what it basically does.  Now remember, the name is a pointer to position one.  This syntax says; take address of matched, go to next position(+ 1), and print it out.  However, when you get into multidimensional array, this becomes a hassle so just use pointer format to pass arrays to functions and from there just use array syntax.

Ok, back to your question.  Now that you understand that array names are pointers, lets send them to your function!  Your declaration so far is:

  void file_report_analyser():

Now why is it void?  Arrays are automatically pass-by-reference.  You change it in the file, it changes the source value.  So, lets send the arrays:

  void file_report_analyser(int*, int*, int*):

Wasn't that easy?  We used pointer syntax to send the arrays to the funtion.  Now lets take a look at the function definition header:

void file_report_analyser(int *matched, int *to_download, int *to_upload)
{ ... }

Really easy.  Notice when using pointer notation to pass arrays you do not have to include their rows or colums values.  However, you should always keep a length variable in tune with them so you don't go outside your array's bounds.  And finally, once the array are sent, how do you access them?

cout << *matched << endl;
cout << matched[0][0] << endl;

Yes, you can use either syntax.  Pointer/Array syntax is interchangable when dealing with arrays.  I hope this solves your problems.

Author

Commented:
Thanks everyone, but especially thanks to Taplar.

Excellent explanation, and you did as i wished, and included syntax. Now i can go and finish this bloody thing.


=)



Cheers all!
Author of the Year 2009

Commented:
dewils80,
There is a little problem you will encounter.  If you use

file_report_analyser(int *matched, int *to_download, int *to_upload)
{ ... }

There will be no straight-forward way to access, say, matched[17][35].  Perhaps Taplar will explain.

-- Dan

Commented:
Yes there will.  Once the array is sent a function in pointer notation, it can be used in either syntax without error.  The whole pupose of being able to use pointer syntax to pass array was just so you wouldn't have to declare the rows or cols when passing them.

Inside the function he will be able to use matched[17][35].
Try it and see.

Commented:
Btw, when calling the function if using my file outline use:

file_report_analyser(matched, to_download, to_upload);

Don't use the * or &.

Commented:
Wish there was an edit option, lol.  To clarify:

// Function Declaration
void file_report_analyser(int*, int*, int*):

// Function Call
file_report_analyser(matched, to_download, to_upload);

// Function Definition
void file_report_analyser(int *matched, int *to_download, int *to_upload)
{ ... }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial