Solved

double array

Posted on 1998-07-03
1
217 Views
Last Modified: 2010-08-05
Hi

I have problems passing a double array to a function.
I wrote two examples.
One involves a simple array. It works.
The second involves a double array.Id doesn.t.


First example

#include<stdio.h>
#include<iostream.h>

void printArray(int*);

int theArray[8];
int anotherArray[8];

void printArray(int* givenArray)
{
      for(int i = 0; i < 8; i++)
      {
            cout << givenArray[i] << endl;
      }
}


int main()
{

      for(int i = 0; i < 8; i++)
      {
            theArray[i] = i;
            anotherArray[i] = 10 + i;
      }

      printArray(theArray);
      cout << endl;
      printArray(anotherArray);

      return 0;
}


Second example:
#include<iostream.h>


void printArray(int**);



int theArray[8][8];
int anotherArray[8][8];

void printArray(int** givenArray)
{
      for(int i = 0; i < 8; i++)

            for(int j = 0; j < 8; j++)

                  cout << givenArray[j][i] << endl;

}

int main()
{
     for(int i = 0; i < 8; i++)
     {
      for(int j = 0; j < 8; j++)

             {
                  theArray[j][i] = i;
                  anotherArray[j][i] = j;
            }
     }



     printArray(theArray);
     cout << endl;
     printArray(anotherArray);

     return 0;
}


The error message that I get is"can not convert int[8]*
to **".

I understand that in the case of a single array,
theArray is equivalent to *theArray[0];

A pointer to the first member of the array.
I think that in case of a double array, we deal with a pointer to the first element of the array wich is an array itself that means is a pointer to the first element of the array.
So in case of a double array we deal with a pointer to a pointer.
When I passed the simple array to the function it was ok, because the function was defined as receiving a pointer to an integer and theArray was a pointer to an integer.

The second time, the function is defined as receiving a pointer to a pointer to an integer. theArray is a pointer to a pointer to a integer, still the program complains.

Thank you.
0
Comment
Question by:simi
1 Comment
 
LVL 1

Accepted Solution

by:
alex_r earned 50 total points
ID: 1167134
When you are dealing with double array[x][y], it's like you are dealing with array[x*y]
(It's wha's the complier do: "converts" array[2][3] to array[2 * y + 3])
So you must "to give" to compiler the "y"

So. you must your define function like this:
void printArray(int givenArray[][8])
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
returning a dereferenced pts in C++ 10 141
Find Visual Studio Tools 2 92
computer science syllabus 3 70
Eclipse IDE - Cannot copy/paste from console output 8 124
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now