Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
the below code there is no issue,
but address not able to get.
please provide your solution.
i have below code

C#
#include<iostream.h>
#include<stdlib.h>
void main()
{
    char **array;
    int row,column;
    char temp='A';
    cout<<"enter the Row"<<endl;
    cin>>row;
    cout<<"enter the Column"<<endl;
    cin>>column;
    array=(char **)malloc(row*sizeof(char *));
    for (int i=0;i<row;i++) {
        array[i]=(char*)malloc(column*sizeof(char));
    }

    for(int j=0;j<row;j++)  {
        for (int k=0;k<column;k++)
        {
            cout<<&array[j][k]<<endl;
            array[j][k]=temp;
            cout<<*(*(array+j)+k)<<endl;
            temp++;
        }
    }
}



[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 3-Feb-13 2:01am
v4
Comments
ashu_1july 3-Feb-13 10:27am    
the above question i want to allocate memeory for 2d array string but not able to do.
where is the wrong please help me.

You seem to have allocated a 2D array of char instead of a 2D array of strings. However this may or may not be what you intended. You've tagged the question as C++ but seem to be writing 'C'. A more C++ way of doing this might include amongst other lines:-

#include <vector>

using namespace std;

vector< vector< string > > ArrayOfArrayOfStrings;
vector< string > FirstArrayOfStrings;
FirstArrayOfStrings.push_back( string("A") );

ArrayOfArrayOfString.push_back( FirstArrayOfStrings );


The good part is all the mallocing gets done for you along with all the freeing so your program doesn't leek memory and there are no more hard coded fixed limits or 'magic' numbers in your code.
The down side is you will need to put in some hours to understand and use iterators to do the outputting of the vector properly.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Feb-13 14:27pm    
Agree, a 5.
—SA
ashu_1july 4-Feb-13 5:24am    
Is there any simplest way to allocate 2d array char memory dynamically, please look the above code & provide your best solution.
Matthew Faithfull 4-Feb-13 5:38am    
There are many ways, the best is to get the standard library to do it for you for the reasons already mentioned. If you really are programming in C, no C++ allowed, then you solution is already similar to anything I would come up with. One change I might make is to get the row and column input values and the simply multiply them together and allocate x*y chars in one malloc char array[ rows * columns ] = malloc( rows * columns * sizeof(char) ); if you want a simplification.
C++
#include <string>
#include <iostream>

int main(int _argc, char* _argv[])
{
    int nX, nY;
    std::cin >> nX;
    std::cin >> nY;

    //create array
    std::string** strArray;
    strArray = new std::string*[nX];
    for(int i = 0; i < nX; i++)
        strArray[i] = new std::string[nY];

    strArray[0][0] = "Hello";
    strArray[nX-1][nY-1] = "World";

    //delete array
    for(int i = 0;  i < nX; i++)
        delete strArray[i];
    delete strArray;
}
 
Share this answer
 
Comments
sja63 4-Feb-13 8:25am    
Hi,

std::string** strArray; // ???

I am a simple structured C/C++ programmer,
the upper line is too complicated for me.

I would prefer solution 2.

And you will get problems with nX = nY = 1.

Best regards

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