You are declaring an array of pointers:
int* b[10];
you need to allocate memory for each of those pointers, namely:
for (int i = 0; i < 10; i++) {
b[i] = malloc(3 * sizeof(int));
}
From Difference between scanf and scanf_s one can read:
scanf originally just reads whatever console input you type and assign
it to a type of variable.
(..)
scanf_s has an argument(parameter) where you can specify the buffer size and actually control the limit of the input so you don't crash the whole building.
Therefore, what you need is scanf
, hence change your code from:
scanf_s("%d", (*(b+i)+j));
to
scanf("%d", (*(b+i)+j));
You should also check the returning value of the scanf
function, and free the memory of the dynamically allocated array.
An example of a full running code:
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
int array_size = 3;
int* b[array_size] ;
// Allocate memory for my array
for (int i = 0; i < array_size; i++)
b[i] = malloc(3 * sizeof(int));
int scanf_value = 0;
for (int i = 0; i < array_size; i++)
for (int j = 0; j < 3; j++){
if(scanf("%d", &b[i][j]) != 1){
while(getchar()!='\n'); // clean the input buffer
printf("Invalid Input! Add just numbers\n");
j--;
}
}
// print those elements
for (int i = 0; i < array_size; i++)
for (int j = 0; j < 3; j++)
printf("%d ",b[i][j]);
// lets free the memory
for (int i = 0; i < array_size; i++)
free(b[i]);
return 0;
}