CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Problem With Function Returning 2-Dim ******* Array

    I made 2 functions one that returns 2-Dim Character Array and the other one returning 2-Dim ******* Array. In case of character array it works but in case of ******* array instead of displaying those numbers in Arrays its showing memory locations. Here is the code.

    #include <iostream.h>
    #include <conio.h>

    char **edit(char **str); // Prototype for 2-Dim Character Array
    int **Dim (int **num); // Prototype for 2-Dim ******* Array

    void main()
    {
    char **str;
    int **num;

    str = new char* [255];
    num = new int* [255];

    edit(str); // Call for 2-Dim Character Array
    Dim (num); // Call for 2-Dim ******* Array

    for(int j=0;j<3;j++)
    {
    cout<<str[j]<<endl;
    cout<<num[j]<<endl; // The Problem lies over here, Address is getting printed
    }
    delete str;
    delete num;
    }

    char **edit(char **str)
    {
    char *pNames[] = {"Maverick","Marlin","Slider"};
    for(int j=0;j<3;j++)
    {
    str[j] = new char[255];
    str[j] = pNames[j];
    }
    return str;
    }

    int **Dim (int **num)
    {
    int pNums[][3] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6};
    for (int i=0; i<3; i++)
    {
    num[i] = new int[20];
    num[i] = pNums[i];
    }
    return num;
    }

    Can anyone help in sorting this?
    Last edited by maverick786us; July 29th, 2005 at 01:04 AM.

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Problem With Function Returning 2-Dim ******* Array

    First of all, please use the code tags, while posing the code, its very hard to read the code, all aligned one sided.

    Next, there are many things that are wrong in the code. You are using depricated header, (although haven't been used).

    Code:
    #include <iostream.h>
    should be
    Code:
    #include <iostream>
    Return type of main should be int. void is non-standard.

    You are using inappropriate deallocation (delete) for 'new[]' data -- Effective C++ #5).

    char ** is a pointer which pointers to another char pointer, its not a 2-D character array.

    You are passing address of auto variable into caller spaces in edit(...) & Dim(...).

    You are not using the return values of the functions, so whats the point of returning any value, if its not going to be used?

    The whole thing can be simplified if you use STL (vector & string) . Is it an assignment? I mean, can you use STL?
    Last edited by Ejaz; July 29th, 2005 at 02:32 AM.

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem With Function Returning 2-Dim ******* Array

    I need to go through it first

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Problem With Function Returning 2-Dim ******* Array

    and this was posted yesterday so it's a repeat post.

  5. #5
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem With Function Returning 2-Dim ******* Array

    No its not a repeat post. I tried the coding given in yesterday's answer and somehow it didn't work, therefore i had to post it.

    Its simple. It is working in case of characters but not *******s.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Problem With Function Returning 2-Dim ******* Array

    Code:
    #include <iostream.h> // wrong header. use <iostream>
    #include <conio.h> // I think this is a non-standard header for keyboard i/o?
    
    char **edit(char **str); // Prototype for 2-Dim Character Array
    /* 
       no it's a prototype for a function "edit" that takes as a 
       parameter a char** and returns  a char**. char** is a 
       pointer to a pointer to a char (or an array of chars). 
       It is not a  pointer to a 2-d array.
    */
    int **Dim (int **num); // Prototype for 2-Dim ******* Array
    /* see above */
    
    void main() // but main() should return int
    {
    char **str;
    int **num;
    
    str = new char* [255]; // now str is an array of 255 pointers. But arrays are "evil"
    num = new int* [255]; // num is an array of 255 pointers.  Pointers are also "evil"
    // (though not quite as evil as arrays).
    
    edit(str); // Call for 2-Dim Character Array
    Dim (num); // Call for 2-Dim ******* Array
    
    for(int j=0;j<3;j++)
    {
    cout<<str[j]<<endl;
    cout<<num[j]<<endl; // The Problem lies over here, Address is getting printed
    // what do you expect it to output?: How do you output an int* ?
    }
    delete str; // wrong form of delete, use delete[]
    delete num; // wrong form of delete. use delete []
    }
    
    char **edit(char **str)
    {
    char *pNames[] = {"Maverick","Marlin","Slider"}; // but they are const char*
    for(int j=0;j<3;j++)
    {
    str[j] = new char[255]; // you call new here but never call delete on this
    str[j] = pNames[j];  // instead you immediately create a leak by assigning the pointer to
     // point to something else. (You presumably intended to use strcpy() )
    }
    return str;
    }
    
    int **Dim (int **num)
    {
    int pNums[][3] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6}; // not of type int[N][3] for any N. (***)
    for (int i=0; i<3; i++)
    {
    num[i] = new int[20];
    num[i] = pNums[i]; // oops I did it again!
    }
    return num;
    }
    I don't know what compiler you are on that even allows you to compile the line where I've marked (***).

    Anyway, unless this is legacy code you have picked up and are trying to do a "quick fix" without breaking anything else, you should consider a rethink about the way you have approached all of this. (I don't know exactly what you are trying to achieve with all these arrays).

    Use the STL collection classes unless you have good reason not to. And there rarely is a reason not to, except for their lack of portability across libraries or networks.

  7. #7
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem With Function Returning 2-Dim ******* Array

    I am using VC++ 6.0 Compiler. OK I will try STL Libraries or Vector in this case thanks

  8. #8
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem With Function Returning 2-Dim ******* Array

    I never had any intention to put this question. It was just that yesterday Mt. HumtyDumpty answered my yesterday's post

    http://www.codeguru.com/forum/showthread.php?t=350868

    and he told me that the same way it is going to work for integer array, but it didn't, thats why i posted this question along with the code

  9. #9
    Join Date
    Apr 2005
    Location
    Mumbai,India
    Posts
    185

    Re: Problem With Function Returning 2-Dim ******* Array

    cout<<str[j]<<endl;
    cout<<num[j]<<endl; // The Problem lies over here, Address is getting printed
    see the definition in your code
    char **str;
    int **num;
    both *num and *str will be address, since *str will point to one dimensional array of character, and it is considered string in C and by printf() , cout, etc. so when writing cout<<str[j] , it prints the string whose first character is str[j][0]. But *num is still is pointer .So writing cout<<num[j], it will print its value that is address. if it were strings, it would be printed its contents.To print integer array, use loop and print each contents indiviaully as num[i][j].
    There are several mistakes in your code, some already pointed out by Ejaz .

    Code:
     char *pNames[] = {"Maverick","Marlin","Slider"};
     for(int j=0;j<3;j++)
     {
     str[j] = new char[255]; ///allocating memory and storing pointer in str[j]
     str[j] = pNames[j];  ///loosing pointer of above memory and overwritten by pName[j], pointer to local object
     }
     You are doing same thing in your other function also.
    See modified code below
    Code:
    #include <iostream>
     #include <cstring>
     using namespace std;
     
     char **edit(char **str); // Prototype for 2-Dim Character Array
     int **Dim (int **num); // Prototype for 2-Dim ******* Array
     
     int main(int argc, char* argv[])
     {
     
     	 char **str;
     	  int **num;
     
     str = new char* [255];
     num = new int* [255];
     
     edit(str); // Call for 2-Dim Character Array
     Dim (num); // Call for 2-Dim ******* Array
     
     for(int j=0;j<3;j++)
     {
     cout<<str[j]<<endl;
     for(int i=0;i<5;i++)
     cout<<num[j][i]<<endl;
     }
     
     for(int i=0;i<3;i++)
     {
     	delete[] str[i];
     	delete[] num[i];
     }
     	delete[] str;
     	delete[] num;
     
     		return 0;
     }
     
     char **edit(char **str)
     {
     char *pNames[] = {"Maverick","Marlin","Slider"};
     for(int j=0;j<3;j++)
     {
     str[j] = new char[255];
     strcpy(str[j],pNames[j]);
     }
     return str;
     }
     
     int **Dim (int **num)
     {
     int pNums[3][5] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6};
     for (int i=0; i<3; i++)
     {
     num[i] = new int[5];
     for(int j=0;j<5;j++)
       num[i][j] = pNums[i][j];
     }
     return num;
     }
    Life is what u make it and u can make it more simple..........and woderful.
    Rate is what give and it give me pleasure.


  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Problem With Function Returning 2-Dim ******* Array

    If possible you should try to upgrade to version 7.0 or later. VC6.0 is not very good with templates and has problems supporting some of the features of the C++ standard.

  11. #11
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Problem With Function Returning 2-Dim ******* Array

    Code:
    int pNums[3][5] = { {3,4,5,6,5},{4,6,1,9,5},{5,1,0,0,6}};
    I think that's the grouping. (It may be 5 groups of 3. I can never remember, probably because I never do it this way ).

    But best avoided anyway.
    [/code]

    Better overall is:
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "matrix.h" // with the matrix from the FAQ.
    #include <cassert>
    
    void edit( std::vector< std::string > & );
    void dim( Matrix<int> & );
    
    int main()
    {
       std::vector<std::string> myStrings;
       Matrix<int> myMatrixOfInts(5,3);
       
       edit( myStrings );
       dim( myMatrixOfInts );
    }
    
    void edit( std::vector< std::string > & v )
    {
       const char *pNames[] = {"Maverick","Marlin","Slider"};
       v.clear();
       v.reserve( 3 );
       std::copy( pNames, pNames+3, std::back_inserter( v ) );
    }
    
    void dim( Matrix<int> & m )
    {
       assert( m.rows() == 3 );
       assert( m.cols() == 5 );
    
       const int inits[] = {3,4,5,6,5,4,6,1,9,5,5,1,0,0,6};
       int* firstel = &m(0,0);
       std::copy( inits, inits+15, firstel );
    }
    Not ideal (and you need the Matrix class) and obviously too much "hard-coded" here, but at least you have no memory-handling issues here.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured