Click here to Skip to main content
Rate this: bad
good
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
 
#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 3-Feb-13 1:51am
Edited 3-Feb-13 2:01am
v4
Comments
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
good
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.
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 3-Feb-13 14:27pm
   
Agree, a 5.
—SA
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
good
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;
}
  Permalink  
Comments
Member 9586495 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
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