Help me with pointer to pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wazzup
    New Member
    • Feb 2007
    • 25

    Help me with pointer to pointers

    I'm working on dynamic arrays. I found source code in C. Please help me convert this line of code from C to C++. Thanks so much for the help.

    row and col are variables

    Code:
    arr=(int**)malloc(sizeof(int)*row*col);
  • lqdeffx
    New Member
    • Mar 2007
    • 39

    #2
    well if you are truly interested in learning on how to create dynamic arrays in C++, I would suggest researching STL containers . Creating dynamic arrays, such as vectors, lists, deques...etc is much safer and is usually efficient enough for most general purposes. Plus comes with lots of little functions like sort or insert.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Since the OP is working on dynamic arrays alone, the STL stuff is probably in the future for his/her coursework (I'm assuming you're taking a class).

      C++ uses the new operator, so the malloc call you have should be

      Code:
      int** arr = new int[rows][cols];
      in C++.

      Comment

      • wazzup
        New Member
        • Feb 2007
        • 25

        #4
        Yeap, I'm taking CS 161, really basic stuff, this is my extra credit project. That's y my instructor wanted us to write functions, should not use vector at this time. Anyways, thanks much for the help!

        Comment

        • wazzup
          New Member
          • Feb 2007
          • 25

          #5
          When I tried put that code in the function, it gave me some errors such as:
          ==> declaration of 'int**arr' shadows a parameter
          ==> `col' cannot appear in a constant-expression

          How can I fix this errors?

          Code:
          /* to initialize matrix */
          int** init(int** arr,int row,int col)
          {
                         int i=0,
                             j=0;
          
          	//arr=(int**)malloc(sizeof(int)*row*col);
          	int** arr = new int[row][col];
          
          	for(i=0;i<row;i++)
          	{
          		for(j=0;j<col;j++)
          		{
          			*((arr+i)+j)=(int*)malloc(sizeof(int));
                      *(*(arr+i)+j)=0; 
          		}
          	}
          	return arr;
          }

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            try replacing
            Code:
            	int** arr = new int[row][col];
            with
            Code:
            	 arr = new int*[row];
            	 for(int i=0;i<row;i++)
            	   arr[i]=new int[col];

            Comment

            • wazzup
              New Member
              • Feb 2007
              • 25

              #7
              The compiler of BloodShed did not warning for errors but program terminate when accessing the function with that replacing. Any ideas? Many thanks for the help so far.

              Comment

              • marvinp
                New Member
                • Feb 2007
                • 8

                #8
                I suggest if u want to create dynamic 2D array


                int rows = 4;
                int cols = 3;

                int **pp = new int*[rows];

                for ( int i =0 ; i < rows ;i ++ )
                {
                pp[i] = new int[cols];
                }

                This will create a dynamic 2D array of rows 4 and cols 3.

                can access the array element using pp[i][j].

                Comment

                Working...