Re: Return 2-Dim Int Array
Re: Return 2-Dim Int Array
Firstly - If this is C++ - use refernce to pointers instead of pointers to pointers. A better way to go would be using vectors.
Secondly - What you are trying to do can be done by simply equating num and adt, provided num is is the same as adt. You are just assigning values into adt and then equating the pointer.
Re: Return 2-Dim Int Array
Quote:
Originally Posted by maverick786us
I made this function to return a 2-Dimensional Integer Aarray at one go but its creating error while assigning it.
#include <iostream.h>
#include <conio.h>
int ** Dim (int ** num);
void main()
{
int adt[3][3];
int num = Dim (&(&adt)); // Error C2102 : & requires l-Value
}
int ** Dim (int ** num)
{
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++)
num[i][j] = i;
}
return num;
}
Is there an alternative way to make a function which could return a 2-Dim Array at one go?
here is a example i am using character string here you can same for integer
//
First thing when you are using Pointer no need to return the value or address you can perform all the calculation directly .because now you are performing all the opration on object only. neither on the copy of object.
Second thing in case of pointer to pointer.
you have to initialize the first dimension of pointer at the time of decleration only.Second upto you when and where you want to initialize.
.after finishing work on pointer don't forget to delete.like i forget.
Code:
char **edit(char **str);
int main()
{
char **str;
str = new char*[255];
edit(str);
for(int j=0;j<3;j++)
{
cout<<str[j]<<endl;
}
}
char **edit(char **str)
{
for(int j=0;j<3;j++)
{
str[j] = new char[255];
str[j] = "hello";
}
return str;
}
Re: Return 2-Dim Int Array
Quote:
Originally Posted by exterminator
First of all - int adt[3][3]; is not a 2-D array. Its a 3-dimensional array, in fact.
Secondly - If this is C++ - use refernce to pointers instead of pointers to pointers. A better way to go would be using vectors.
Thirdly - What you are trying to do can be done by simply equating num and adt, provided num is is the same as adt. You are just assigning values into adt and then equating the pointer.
okay
please let me know how this is a 3d array,just read some good tutorial
if this is 3d Array
then what is
int a[2][2][2]
i think u r confused with 3
that tell no of rows and column of array
Re: Return 2-Dim Int Array
Quote:
Originally Posted by humptydumpty
okay
please let me know how this is a 3d array,just read some good tutorial
if this is 3d Array
then what is
int a[2][2][2]
i think u r confused with 3
that tell no of rows and column of array
Aaahh..okay..My mistake .. thanks for correcting me .. I guess I confused it with matrices...it sounded like a 3x3 matrix, (which it is, in fact) and then I did this. I corrected it in my earlier post.
Hey Humpty, by the way, your idea about deleting something that was new-ed somewhere else is not a good idea. The method provided by Golanshahar in his link is a better one. By the way... read this FAQ
Re: Return 2-Dim Int Array
maverick786us, for informations on bidimensional array, got to this FAQ http://www.parashift.com/c++-faq-lit...html#faq-16.16
I suppose you tried to write a code like that:
Code:
#include <iostream> // use iostream instead of iostream.h
void Dim (int num[3][3]);
int main() // void main is incorrect in C++
{
int adt[3][3];
Dim (adt); // initialize the matrix
for(int j=0;j<3;j++) // outputs the matrix
{
for(int i=0;i<3;i++)
std::cout<<adt[i][j]<<" ";
std::cout<<std::endl;
}
return 0;
}
void Dim (int num[3][3])
{
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++)
num[i][j] = i;
}
}
But static arrays should be avoided.
In fact, you can write a matrix class using a static 3x3 bidimensional array, to do fast computing on it. Such matrixes can be useful in real-world applications.