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;
}

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the guide , well i was indeed missing out on the indirection operator , but didn't know about allocation of memory of pointer
If the issue is resolved, please mark the answer as accepted to close it. @yugsharma1711
Actually it needs 5 minutes for marking , i will definitely do it
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;
}

Comments

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().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.