Return 2-Dim Int Array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Return 2-Dim Int Array

  1. #1
    Join Date
    Apr 2005
    Posts
    1,750

    Return 2-Dim Int Array

    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?

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: Return 2-Dim Int Array

    read this Thread

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.
    Last edited by exterminator; July 28th, 2005 at 08:08 AM. Reason: Made a correction as pointed out later

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Lightbulb 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;		
    }

  5. #5
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,716

    Lightbulb 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
    Last edited by humptydumpty; July 28th, 2005 at 07:50 AM.

  6. #6
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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
    Last edited by exterminator; July 28th, 2005 at 08:07 AM.

  7. #7
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center