| | |
Passing 2D Array of Pointers into a function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2005
Posts: 5
Reputation:
Solved Threads: 0
Dear All,
I am facing a problem in passing 2D array of pointers to a function.
can any one give some suggestions on this.
1. How the Declaration fo the function should be ?
2. How to pass 2D array of pointers to that function?
3. How to access the array elements.
The aray declaration is *ptr[2][3] = { {"AA","XX","FF"},
{"DD","QQ","EE"},
};
It would be great helpful for me,since I am struggling with this one
I am facing a problem in passing 2D array of pointers to a function.
can any one give some suggestions on this.
1. How the Declaration fo the function should be ?
2. How to pass 2D array of pointers to that function?
3. How to access the array elements.
The aray declaration is *ptr[2][3] = { {"AA","XX","FF"},
{"DD","QQ","EE"},
};
It would be great helpful for me,since I am struggling with this one
If you're struggling, duplicate your declaration:
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.
C++ Syntax (Toggle Plain Text)
void foo ( int *arg[2][3] ); int main ( void ) { int *ptr[2][3]; foo ( ptr ); return 0; }
I'm here to prove you wrong.
•
•
Join Date: Sep 2005
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
If you're struggling, duplicate your declaration:
Within foo, you use arg just as you would use ptr unless you're trying to use sizeof, in which case you probably won't get the result you want.C++ Syntax (Toggle Plain Text)
void foo ( int *arg[2][3] ); int main ( void ) { int *ptr[2][3]; foo ( ptr ); return 0; }
Hi,
Thanks for your reply.
But I have diffrent types of 2D array of pointers which varies rows and columns size, So in the function declaration/definition I can not specify the rows and columns size.
So I am trying for an alternative.
>So in the function declaration/definition I can not specify the rows and columns size.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:
You have more options in C++, but you neglected to mention what language you're using.
Then you can't use an array. The size of the first dimension may be omitted, but the others are required, and the size of an array dimension must be a compile-time constant. If you want to pass a two dimensional array of any size to a function, you use dynamic memory:
C++ Syntax (Toggle Plain Text)
#include <stdlib.h> void foo ( int ***arg ); int main ( void ) { int ***ptr = malloc ( 2 * sizeof *ptr ); int i; for ( i = 0; i < 2; i++ ) ptr[i] = malloc ( 3 * sizeof *ptr[i] ); foo ( ptr ); for ( i = 0; i < 2; i++ ) free ( ptr[i] ); free ( ptr ); return 0; }
I'm here to prove you wrong.
here is how you initialize the function and pass the values
c++ Syntax (Toggle Plain Text)
char *function(char **array) { //your code here return 0; } int main() { //put values in array function(p); //passing to function }
Maybe you want to take a look at my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void func(char **& ref_to_ptr); /* Function declaration */ int main(void) { /* Declare the '2D Array' */ char ** ptr = new char * [5]; ptr[3] = new char[20]; /* Put some data in the array */ ptr[3] = "k"; /* Print the first value on the screen */ cout << "First value: " << ptr[3] << endl; /* Pass the array by reference to the function 'func()' */ func(ptr); /* Again we print on the screen what's in the '2D Array' */ cout << "Second value: " << ptr[3] << endl; /* Wait for the user to press ENTER */ cin.get(); /* Cleanup */ delete[] ptr; /* Tell the Operating System that everything went well */ return 0; } void func(char **& ref_to_ptr) { /* This function demonstrates how to change the value of it's argument(s) */ ref_to_ptr[3] = "tux4life"; }
Last edited by tux4life; Mar 11th, 2009 at 3:40 pm.
![]() |
Similar Threads
- select sort using array of pointers---getting wrong values. (C++)
- Deleting Characters Function (C)
- Passing a Value between an Array and Another Function (C++)
- Passing array of structures into a function (C++)
Other Threads in the C++ Forum
- Previous Thread: Usage of array of pointers
- Next Thread: How To Return to Main()
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






