0

while i am trying to solve a hackerrank problem,i had a problem in my mind.My problem is i want to allocate some memories with pointers and i made this with two ways,but one of them resulted with the problem.Other one has no problem.

Hackerrank problem is here 1. enter image description here

Hackerrank problem is here 2.enter image description here

Hackerrank problem is here 3.enter image description here

This code worked with no problem.

int x,y,z;
cin>>x>>y;
int *arr[x];
for(int i=0;i<x;i++){
    int z;
    cin>>z;
    arr[i] = new int[z];
    for(int j=0;j<z;j++){
        cin >> arr[i][j]; 
    }
    
}
int *arr2[y];
for(int k=0;k<y;k++){
    arr2[k] = new int[2];
    for(int tt=0;tt<2;tt++){
        cin >> arr2[k][tt];
    }
    
}
for(int i = 0;i<y;i++){
    
        cout<<arr[arr2[i][0]][arr2[i][1]]<<endl;
    
}

My question is here.

**what is the problemlem for making a definition of int *arr = new int[x]; rather than *arr[x] I want to make a new allocation with pointer.Is both of them aren't same ? I didn't understand. ** *If a make a definition of int arr = new int[x]; then the problem is arr[i] = new int[z];

int x,y,z;
cin>>x>>y;
int *arr = new int[x];  **<-- **
for(int i=0;i<x;i++){
    int z;
    cin>>z;
    arr[i] = new int[z];   **<-- problem**
    for(int j=0;j<z;j++){
        cin >> *arr[i][j]; 
    }
    
}
int *arr2 = new int[y];
for(int k=0;k<y;k++){
    arr2[k] = new int[2];
    for(int tt=0;tt<2;tt++){
        cin >> *arr2[k][tt];
    }
    
}
for(int i = 0;i<y;i++){
    
        cout<<*arr[*arr2[i][0]][*arr2[i][1]]<<endl;
    
}


  
return 0;
angela
  • 3
  • 2

1 Answers1

1

You are trying to make a matrix (2-dimensional array) from an array. You should write like that:

int **matrix = new int*[x]; //pay attention to number of asterisks
for(int i = 0; i < x; ++i)
{
  matrix[i] = new int[z];
}

int *arr[x] is similar to int **matrix = new int*[x]. It creates an array of x arrays with undefined size.

P.S: using pointers in C++ is considered a bad practice. You should use std::vector instead. That's an example:

std::vector<std::vector<int>> matrix(x);
for(int i = 0; i < x; ++i)
{
  matrix[i] = std::vector<int>(z);
}

P.P.S: don't forget to #include <vector>.

gavrilikhin.d
  • 554
  • 1
  • 7
  • 20
  • This became so helpful for me and i understood a lot with the help of you.Thank you so much :D – angela Dec 01 '20 at 13:41
  • I'm glad to help you! – gavrilikhin.d Dec 01 '20 at 13:42
  • However,i didn't understand vectors.In which situations should i use vectors instead of using pointers in memory allocation ? Why should i use vectors? – angela Dec 01 '20 at 13:43
  • 1
    `std::vector` is nothing more than a dynamically allocated array. In C++ you almost never use raw pointers for the purposes of creating a collection of data. `std::vector` is safer (don't need to free memory by yourself) and more convenient (you are able to use some interesting premade stuff with it in C++). – gavrilikhin.d Dec 01 '20 at 13:47
  • Thanks a lot again,have a nice day! – angela Dec 01 '20 at 13:52
  • I have some litte problem again,can you explain for me ? Why can't i use cin >> *arr2[k][tt]; (* in front of arr2[k] or others like this example). I had one queston. If i don't use * , is it become address ? if i don't use * , won't i change its addresses ? After define a pointer can't i use *ptr instead ptr in order to assign a value to it ? – angela Dec 01 '20 at 14:00
  • Could you please provide some more context? – gavrilikhin.d Dec 01 '20 at 14:03
  • Actually more context is in the post question. I made a problem while solving it . (I used * some areas in the question if you look.) And i made a compile error. – angela Dec 01 '20 at 14:06
  • 1
    `cin >> *arr[i][j];` is wrong because `arr[i][j]` is an `int` not a pointer. You want `arr[i][j]` instead. – drescherjm Dec 01 '20 at 14:08
  • No, it's not an addres. To understand it you need to know what `array[i]` really means. `int *array` is a pointer to a memory location with a size of `int` (usually 4 bytes). And `array[i]` is an **element at address** of `array + i*sizeof(int)`. – gavrilikhin.d Dec 01 '20 at 14:11