1

The following code is the answer that I found to this challenge

https://www.hackerrank.com/challenges/vector-sort/problem?isFullScreen=true&h_r=next-challenge&h_v=zen

int main() {
      
    int x,y,s=0;
    cin>>x>>y;
    int *arr[x];
    while(x--)
    {
        int num;
        cin>>num;
        arr[s]=new int[num];
        for(int i = 0; i<num ; i++)
        {
            cin>>arr[s][i];
        }
        s++;
    }
    while(y--)
    {
        int a,b;
        cin>>a>>b;
        cout<<arr[a][b];
        cout<<"\n";
    }
    
     return 0;
}

My question is how "arr" which is a pointer array can store values of the array elements in line 13 and also print the array elements in line 21.

To use the value stored in the array we must use an asterisk right? Then how is it possible to scan and print elements in the pointer array without an asterisk as a prefix?

AxXxelWolf
  • 87
  • 6
  • `int *arr[x];` is not standard C++: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). I suggest you to learn about standard portable C++ before you dive into fancy compiler extensions, that would be `std::vector arr(x);` or `std::vector>` for 2d – 463035818_is_not_an_ai Mar 19 '21 at 11:38

2 Answers2

0
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
      
    int size  ;
    cin>>size;
    int *array=new int [size];
 
 
 
 for(int i=0;i<size;i++){
    
 cout<< array[size]<<endl; //it will print zero beccause by default all values set to zero
    
    
 }   
}
      
      

so in this way we can use pointers. To declare pointer array

-1

N3337 5.2.1 Subscripting says:

The expression E1[E2] is identical (by definition) to *((E1)+(E2))

Therefore, arr[s][i] means *(*(arr + s) + i) and it can do what can be done with asterrisks.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70