Hi all,
I have a double pointer in C as follows:

void foo(char * , char**);
main( )
{
    char * inbuff = {0x10,0x20};
    char * outbuff;
    foo(inbuff, (char **) &outbuff);
}
void foo(char *inbuff, char ** outbuff);
{
    
    //code to manipulate inbuff and outbuff.
    *outbuff = (char *) malloc(20);
}

Now I need to convert this in to C++ ? How do I do that?
How do I pass the double pointer as an argument in C++?
and can I do *outbuff in C++?

Any help would be really nice.

Regards,
Prashanth

Recommended Answers

All 4 Replies

1) pass the second parameter by reference using the & operator void foo(char * , char*&); 2) replace malloc() with new

As a matter of fact, your code isn't valid C either. If you are going to ask for help in converting C code to C++, it helps if you post working C code.

You need to read this thread before posting again.

I'm pretty sure that you can't cast with brackets like in C or C# in C++. You will need to static_cast<data_type>(object/variable) it. Also, if you want to use outbuff as a pointer to a pointer, then it should be declared as one (char ** outbuff). I'm not to sure what it is exactly that you are trying to accomplish, are you trying to dynamically add null terminated c style strings to the outbuff array? If so then the double pointer is required, but if you aren't making an array then a single pointer will suffice.

#include<iostream>
using namespace std;
int main()
{
    int r1,c1,r2,c2;
    cout<<"plzz enter the rows n coloumns of  1st matrix respectively"<<endl;
    cin>>r1;
    cin>>c1;

    int **m1=new int *[r1];

    for(int i=0;i<r1;i++)
    {
        m1[i]=new int[c1];
    }

    cout<<"now enter the rows n coloumns of second matrix respectively"<<endl;
    cin>>r2;
    cin>>c2;

    int **m2=new int *[r2];
    for(int y=0;y<r2;++y)
    {
        m2[y]=new int[c2];
    }

    if(c1==r2)
    {
        cout<<"now enter the first matrix row by row"<<endl;
        for(int g=0;g<r1;g++)
        {
            for(int f=0;f<c1;f++)
            {
                cin>>m1[g][f];
            }
        }


        cout<<"now enter the second matrix row by row"<<endl;
        for(int h=0;h<r2;h++)
        {
            for(int w=0;w<c2;w++)
            {
                cin>>m2[h][w];
            }
        }

        int **result=new int *[r1];
        for(int r=0;r<r1;++r)
        {
            result[r]=new int[c2];
        }

        for(int i=0;i<r1;i++)
        {            
            for(int j=0;j<c2;j++)
            {
                result[i][j]=0;
                for(int k=0;k<r1;k++)
                {
                    result[i][j]+=m1[i][k]*m2[k][j];
                }
            }
        }
        cout<<"the resultant matrix is :"<<endl;

        for(int u=0;u<r1;u++)
        {
            for(int p=0;p<c2;p++)
                cout<<result[u][p]<<" ";

            cout<<endl;
        }

        for(int q=0;q<r1;q++)
        {
            delete []result[q];
        }
        delete result;
    }
    else
        cout<<"multiplication cannot be done because coloumns of first matrix is not equall to rows of second coloumns"<<endl;


    for(int a=0;a<r1;a++)
        {
            delete []m1[a];
        }
        delete m1;

        for(int z=0;z<r1;z++)
        {
            delete []m2[z];
        }
        delete m2;


    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.