Continuing from the comments, if the exercise involves a Variable Length Array, then it should be written in C because the C++ standard does not provide VLAs (and they have been made optional beginning in C11 after being added with C99)
The primary problem with your code (aside from a complete failure to allocate the inputs) is that it invokes Undefined Behavior because int tmp_arr[tmp]
goes out of scope at the end of the for
loop. What you assign with p_arr[i] = tmp_arr;
no longer exists outside the loop because tmp_arr
no longer exists. Any attempt to access a value after it has gone out of scope invokes Undefined Behavior, see Undefined, unspecified and implementation-defined behavior
Every time you handle input, you must validate the input succeeded and the value obtained is within a valid range. For instance both n
and q
must be positive values. Using scanf
you could do (minimally)
if (scanf("%d%d", &n, &q) != 2 || n <= 0 || q <= 0) {
fputs ("error: invalid format or value.\n", stderr);
return 1;
}
The above validates that 2 inputs were received and that both are positive values.
In order to preserve the pointer assigned with p_arr[i] = tmp_arr;
, tmp_arr
must be an allocated type to handle an unknown tmp
number of elements, or it must be declared outside the loop (and large enough to handle all anticipated values -- which given that tmp
is read as input -- doesn't seem like the intended approach). You cannot declare tmp_arr
static either as there would only be one instance and assigning it repeatedly to p_arr[i]
would leave all elements of p_arr[i]
pointing to the same place.
Simply allocate for tmp_arr
and then the assignment to p_arr[i]
will survive for the life of the program or until it is freed, e.g.
int *tmp_arr = calloc (tmp, sizeof *tmp_arr); /* allocate */
if (!tmp_arr) { /* validate every allocation */
perror ("calloc-tmp_arr");
return 1;
}
p_arr[i] = tmp_arr;
(note: calloc
was used above to zero the new memory allocated. You can use malloc
instead since the code assigns to each element)
Putting it altogether and adding (minimal) validation, you could do something similar to:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int n, q; /* validate EVERY input */
if (scanf("%d%d", &n, &q) != 2 || n <= 0 || q <= 0) {
fputs ("error: invalid format or value.\n", stderr);
return 1;
}
int *p_arr[n]; /* array of n-pointers to int */
for (int i = 0; i < n; i++) {
int tmp;
if (scanf("%d", &tmp) != 1 || tmp <= 0) { /* validate! */
fputs ("error: invalid input - tmp.\n", stderr);
return 1;
}
int *tmp_arr = calloc (tmp, sizeof *tmp_arr); /* allocate */
if (!tmp_arr) { /* validate every allocation */
perror ("calloc-tmp_arr");
return 1;
}
p_arr[i] = tmp_arr;
for (int j = 0; j < tmp; j++) {
int value;
if (scanf("%d", &value) != 1) { /* validate! */
fputs ("error: invalid input - value.\n", stderr);
return 1;
}
p_arr[i][j] = value;
printf ("%d ", p_arr[i][j]);
}
putchar ('\n'); /* no need to printf a single-character */
}
for (int i = 0; i < q; i++) {
int row, col; /* validate! */
if (scanf ("%d%d", &row, &col) != 2 || row < 0 || col < 0) {
fputs ("error: invalid input, row or col.\n", stderr);
return 1;
}
printf ("%d %d\n%d\n", row, col, p_arr[row][col]);
}
for (int i = 0; i < n; i++)
free (p_arr[i]);
return 0;
}
(Note: untested as no hackerrank input was provided)
Look things over and let me know if you have further questions.