Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have generate 3d array of pointers but could not send it sucessfully to a function

C#
// testPointer.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<stdio.h>
#include <iostream>                 // for i/o functions

using namespace std ;

void pp(int *xx[3][4][5]);
int i,j,k;
//----------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    int *x[3][4][5];
    for(i=0;i<3;i++) for(j=0;j<4;j++) for(k=0;k<5;k++)
    *x[i][j][k]=i+j+k;

    pp(x);//<---Error
    return 0;
}
//------------------------------------------
void pp(int *xx[3][4][5])
{
for(i=0;i<3;i++) for(j=0;j<4;j++) for(k=0;k<5;k++)
cout<<"*xx["<<i<<"]"<<*xx[i][j][k]<<endl;
}
Posted
Updated 13-Mar-11 6:32am
v2

No one of the solutions above are typesafe like this:

XML
#include <stdio.h>
#include <tchar.h>
#include <conio.h>
template <class A>
void MyPrintArray ( A& array )
{
  int  i,j,k;
  for(i=0;i<(sizeof(array)/sizeof(array[0]));i++)
  {
    for(j=0;j<(sizeof(array[i])/sizeof(array[i][0]));j++)
    {
      for(k=0;k<(sizeof(array[i][j])/sizeof(array[i][j][0]));k++)
      {
        _tprintf(__T("%i,%i,%i = %i\r\n"),i,j,k,array[i][j][k]);
      }
    }
  }
}
template <class A>
void MyFillArray( A& array )
{
  int      i,j,k;
  for(i=0;i<(sizeof(array)/sizeof(array[0]));i++)
  {
    for(j=0;j<(sizeof(array[i])/sizeof(array[i][0]));j++)
    {
      for(k=0;k<(sizeof(array[i][j])/sizeof(array[i][j][0]));k++)
      {
        array[i][j][k] = i+j+k;
      }
    }
  }
}
int _tmain(int argc, _TCHAR* argv[])
{
  int      aTest[4][5][6];
  int      bTest[6][5][4];
  _tprintf(__T("--- aTest ----\r\n"));
  MyFillArray( aTest );
  MyPrintArray( aTest );
  _tprintf(__T("--- bTest ----\r\n"));
  MyFillArray( bTest );
  MyPrintArray( bTest );
  _getch();
  return 0;
}
 
Share this answer
 
v2
Have I seen this question before?
how to send 2 dimension array as a parameter to a function[^]

I hope this clears things up a bit
C++
// testPointer.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<stdio.h>
#include <iostream>                 // for i/o functions

using namespace std ;

void pp1(int *xx[][4][5]);
void pp2(int **xx);
//----------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    int i,j,k,l=0;

    int *x[3][4][5]; // array of pointers - you could have used int *x[3*4*5]  
    int values[3*4*5] = {0,}; // Pointers need to point somewhere
    for(i=0;i<3;i++)
    {
        for(j=0;j<4;j++)
        {
            for(k=0;k<5;k++)
            {
                x[i][j][k] = &values[l++]; // Let the pointers point to something
                *x[i][j][k]=i+j+k; // assign a value
            }
        }
    }

    pp1(x);
    pp2((int **)x);
    return 0;
}
//------------------------------------------
void pp1(int *xx[][4][5])
{
    int i,j,k;

    for(i=0;i<3;i++)
    {
        for(j=0;j<4;j++)
        {
            for(k=0;k<5;k++)
            {
                cout<<"*xx["<<i<<"]"<<*xx[i][j][k]<<endl;
            }
        }
    }
}

void pp2(int **xx)
{
    int i,j,k;

    for(i=0;i<3;i++)
    {
        for(j=0;j<4;j++)
        {
            for(k=0;k<5;k++)
            {
                cout<<"*xx["<<i<<"]"<<*xx[i*j*k]<<endl;
            }
        }
    }
}


As you can see the internals of pp1 and pp2 is the same - using [3][4][5] is just the same as [3*4*5] as far as the compiled code is concerned it's just a buffer in memory.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Khalid Sabtan 13-Mar-11 14:35pm    
yesterday i posted a similar question but it was about an array (how to send array to a function)
today i want to send an array of pointers,not an array of integer.
Sergey Alexandrovich Kryukov 13-Mar-11 15:19pm    
Khalid, it only the indication of that you ignore or fail to understand that Answer. You ought to understand this: its pointless to move forward before you understand a basic thing.
--SA
Sergey Alexandrovich Kryukov 13-Mar-11 15:20pm    
My 5 for this and you past Answer, Espen.
--SA
Espen Harlinn 13-Mar-11 15:23pm    
Thanks SAKryukov :)
Khalid Sabtan 13-Mar-11 15:49pm    
seems its new lesson 2 me,i used to be a c++ programmer long time ago now i am in c#.i think i have to fill the array with valuse (like address not integer)before i assigne valuse to the pointers.what do think mr Harlin? am i wright?
void pp(int xx[][4][5]);
int i,j,k;
//----------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    int x[3][4][5];
    for(i=0;i<3;i++) for(j=0;j<4;j++) for(k=0;k<5;k++)
    x[i][j][k]=i+j+k;
    pp(x);//<
    return 0;
}
//------------------------------------------
void pp(int xx[][4][5])
{
for(i=0;i<3;i++) for(j=0;j<4;j++) for(k=0;k<5;k++)
cout<<"xx["<<i<<"]"<<xx[i][j][k]<<endl;
}
 
Share this answer
 
v2
Comments
Khalid Sabtan 13-Mar-11 14:41pm    
you did nor read my question correctly ,the above is not my question , where is the star before the x , i mean array of pointers .
i mean *x[3][4][5] not x[3][4][5]
Sergey Alexandrovich Kryukov 13-Mar-11 15:22pm    
Not a valid argument. Why do you think you need the pointers? Do you really understand the difference?
--SA
Sergey Alexandrovich Kryukov 13-Mar-11 15:22pm    
Hans, my 5.
--SA
Dalek Dave 13-Mar-11 17:12pm    
Good Call, Hans.
Ah! the dreaded C++, ok look, you got several issues there. For starters let us see a basic multidimentional array

C++
#define WIDTH 3
#define HEIGHT 3

int size [HEIGHT][WIDTH];


you can define it as a function parameter like this

C++
void MyMagicFunction ( int array[2][3][4] );  // prototype
void MyMagicFunction ( int array[2][3][4] ) 
{  // definition
    //Do something
}


Then call it like this

int aTest[2][3][4];
MyMagicFunction myFunc( aTest );


By no means this is the best and preferred way. It just makes the code that much complicated and ugly. May be you can use a struct to hold your multidimensional array or even better look for vector.
 
Share this answer
 
v2
Comments
Dalek Dave 13-Mar-11 12:33pm    
Good answer
Sergey Alexandrovich Kryukov 13-Mar-11 15:23pm    
A good Answer showing how to make the dreaded... well less dreaded :-) My 5,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900