Click here to Skip to main content
11,645,087 members (64,208 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C++
the below code there is no issue,
but address not able to get.
please provide your solution.
i have below code

void main()
    char **array;
    int row,column;
    char temp='A';
    cout<<"enter the Row"<<endl;
    cout<<"enter the Column"<<endl;
    array=(char **)malloc(row*sizeof(char *));
    for (int i=0;i<row;i++) {
    for(int j=0;j<row;j++)  {
        for (int k=0;k<column;k++)

[edit]Code block added - OriginalGriff[/edit]
Posted 3-Feb-13 1:51am
Edited 3-Feb-13 2:01am
ashu_1july at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Sergey Alexandrovich Kryukov at 3-Feb-13 14:27pm
Agree, a 5.
ashu_1july at 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 at 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

#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;
Member 9586495 at 4-Feb-13 8:25am

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)

  Print Answers RSS
0 OriginalGriff 700
1 494
2 Sergey Alexandrovich Kryukov 449
3 Afzaal Ahmad Zeeshan 444
4 CPallini 426
0 OriginalGriff 1,065
1 Sergey Alexandrovich Kryukov 703
2 Afzaal Ahmad Zeeshan 702
3 DamithSL 681
4 CPallini 595

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100