0

What's the meaning of the code below?

int **matrix = new int*[n]

What's the difference here between matrix and int*[n]?

3
  • 5
    I would suggest grabbing a beginner’s C++ book or a tutorial to learn the basic syntax of C++. The question about difference doesn’t really make sense. Commented Oct 13, 2018 at 7:06
  • Thnkq for the suggestion.I'm new to stack overflow, I'll take care from next time.Thnks Commented Oct 13, 2018 at 8:25
  • thanks for the question . it clarified my doubt . Commented Nov 28, 2020 at 16:08

7 Answers 7

1

It is an array of 'n' pointers, for which memory can be allocated and initialized in loop. If n is 3, it is an array of 3 elements and each is pointer to int, can point to set of array of integer values like below.

            matrix[0] -> Ox001  points to  array of int [ 1 2 3 4]
            matrix[1] -> Ox017                          [ 5 6 7 8]
            matrix[2] -> Ox024                          [ 9 10 11 12]

Sample code like this

           int **m = new int*[3];
           for(auto i=0; i < 3; i++)
           {
               m[i] = new int[3];
               for(auto j=0; j < 3; j++)
                   m[i][j] = 0;
           }


           for(auto i=0; i < 3; i++)
           {
               m[i] = new int[3];
               for(auto j=0; j < 3; j++)
                   cout << m[i][j];
               cout << "\n";
           }
Sign up to request clarification or add additional context in comments.

Comments

1

for instance there is cricket team and you need

Since you have Cricket* team;, this indicates you have one of two possible

situations:

1) a pointer to a single CricketPlayer (or any derived) type

2) a pointer to an array of CricketPlayer (but not derived) types.

What you want is a pointer to an array of CricketPlayer or derived types. So you

need the **.

You'll also need to allocate each team member individually and assign them to the array:

            // 5 players on this team

          CricketPlayer** team = new CricketPlayer*[5];

          // first one is a bowler

          team[0] = new Bowler();

         // second one is a hitter

          team[1] = new Hitter();

           // etc

// then to deallocate memory

         delete team[0];
         delete team[1];
         delete[] team;

In your query,

           It can be understood as

              int *matrix[]=new int*[n];

SO there are n pointers pointing to n places.

Because

               int *foo;

                foo=new int[5];

will also create 5 consecutive places but it is the same pointer.

see this

In our case it is array of pointers

Comments

1

You need to notice some thing as follows:

  • int *p means that p is a pointer that points to an int variable or points to an array of int variables.
  • int* *p means that p is a pointer that points to an int* variable or points to an array of int* variables.
  • new int[5] is an array of 5 int variables.
  • new int*[5] is an array of 5 int* variables.

In this case, matrix is the second type so it can point to an array of int* variables so the statement:int **matrix = new int*[n] is legal

Comments

1

It means that you have declared a double pointer(of type int) called matrix and allocated an array of n int* pointers.
Now you can use matrix[i] as an int* pointer (0 <= i < n).
Later you might want to allocate memory to individual pointers as well like matrix[i] = new int[size];(size is an int or more appropriately size_t)

Comments

0

matrix is an object of type int **. This type is a pointer to pointer to int.

On the other hand, int * is a type, a pointer to int.

The expression:

new int*[n]

creates n adjacent objects of type int * somewhere in memory and returns a pointer to the first of them. In other words, it allocates and constructs n pointers to int; and returns a pointer to the first. Since the first (and each of them) are int *, the pointer pointing to the first is, in turn, of type int **.

Comments

0

It would be absolutely clear if you remember the operator associativity and precedence.

Going by that int *[n] will be interpreted by compiler : as array of " n pointer to integers".(as [ ] has greater precedence than *, so for easy understanding, compiler interprets it that way.).

Now comming to other side int ** matrix is pointer to pointer. When you created an array of pointer,you basically have the address of the first location ,here first location stores an pointer to integer.

So,when you are storing the address of the first address of such array you will need pointer to a pointer to point the whole array.

Comments

0

//code for passing matrix as pointer and returning as a pointer

 int ** update(int ** mat)
{
    mat[0][1]=3;


    return mat;
}


int main()
{   int k=4;
    int **mat = new int*[k];

           for(auto i=0; i < k; i++)
           {
               mat[i] = new int[k];
               for(int j=0;j<k;j++)
               {
                   if(i==j)
                     mat[i][j]=1;
                   else
                    mat[i][j]=0;
               }

           }

          mat=update(mat);
           for(auto i=0; i < k; i++)
           {

               for(auto j=0; j < k; j++)
                   cout << mat[i][j];
               cout << "\n";
           }
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.