1

I learnt about the concept of array of pointers and ragged arrays, but i'm not able to understand why my code isn't working.

#include<stdio.h>

int main(void)
{
    int* b[10] ;
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            scanf_s("%d", (*(b+i)+j));
        }
    }
    return 0;
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
yugsharma1711
  • 71
  • 1
  • 9

3 Answers3

2

There are multiple issues with your code, so I will just post the corrected one with explanations:

int *b[10];
for (int i = 0; i < 3; i++) {
    // you need to allocate memory to each pointer inside your array as well
    // I have allocated for 3, since you scan 3 integers
    b[i] = malloc(3 * sizeof(int));
    for (int j = 0; j < 3; j++) {
         // use scanf like this, you need the address not the value of variable you are scanning
         scanf("%d", (*(b + i) + j));
     }
 }

You need to include the header file for malloc.

#include <stdlib.h>

Also, don't forget to free the dynamically allocated memory later to avoid memory leaks.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
1

scanf_s takes an address of a variable but you are passing a value. Doesn't your compiler complain? If not, bump up the warning/errors levels. Others already told you that you need to allocate memory for the array pointers to point at. If this was production code remember to free().

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
1

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;
}
dreamcrash
  • 47,137
  • 25
  • 94
  • 117