Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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:
#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:
#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 1-Mar-13 21:43pm
Edited 1-Mar-13 22:17pm
v3
Comments
OriginalGriff at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First of, your C++ implementation should use
#include <iostream> // without the .h!
 
Then, I assume the trouble you have is caused by the line
             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:
             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.
  Permalink  
v2
Comments
Nelek at 2-Mar-13 5:23am
   
Have a look, your first code snippet has a autogenerated "closing-html" tag for your iostream
nv3 at 2-Mar-13 5:42am
   
Thanks!
Nelek at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 2 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid