Click here to Skip to main content
15,886,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create memory for 2D char array dynamically, the same code is working fine in C but its not working in C++.

I have two question:
1. Why C code is working fine & C++ not for 2D char array dynamically memory allocation?
2. If C++ code is wrong or I missed out anything in the C++ code; please correct me. Tell me the actual implementation to allocate 2D char array dynamically.

Please look the below both (C/C++)code.

C code:
C#
#include<stdio.h>
#include<stdlib.h>
void main()
{
    char **array;
    int row,column;
    char temp='A';
    printf("enter the row");
    scanf("%d",&row);
    printf("enter the column");
    scanf("%d",&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++)
        {
            printf("%d\n",&array[j][k]);

            array[j][k]=temp;
            printf("%c\n",*(*(array+j)+k));

            temp++;
        }
    }
}

C++ 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 1-Mar-13 22:17pm
v3
Comments
OriginalGriff 2-Mar-13 3:54am    
"the same code is working fine in C but its not working in C++"

How is it "not working"? What is it doing that it shouldn't, or not doing that it should?
Does it give you an error?

1 solution

First of, your C++ implementation should use
C++
#include <iostream> // without the .h!


Then, I assume the trouble you have is caused by the line
C++
cout<<&array[j][k]<<endl;

The good thing about formatted stream i/o is that it automatically detects the type of variable your are writing and converts them to text accordingly. In this case this automatism however doesn't do what you expected. You are passing a value of type char* and that means, the stream i/o will assume that you want to output a character string. So it writes whatever is in your yet uninitialized array until it finds a NUL-character.

What you intended to do is write that char* as address value, which you may accomplish by saying:
C++
cout << (size_t) &array[j][k] << endl;

That will make you intention clear and you get what you want. By the way, you could have left your printf statements as they were and they would have worked fine in C++ also.
 
Share this answer
 
v2
Comments
Nelek 2-Mar-13 5:23am    
Have a look, your first code snippet has a autogenerated "closing-html" tag for your iostream
nv3 2-Mar-13 5:42am    
Thanks!
Nelek 2-Mar-13 5:44am    
You are welcome. I could have edited it, but I thought you would like to know about it and do it yourself :)

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