Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

double array

Posted on 1998-07-03
1
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 1

Accepted Solution

by:
alex_r earned 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

636 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