I was having a problem with this part while doing one of my assignments. So I decided to do a simple program to store and see if my understanding is correct but I am still having the same problems.

Initially, I didn't make the arrays pointers but that caused the program to crash immediately. So that is also why they are char pointers. If anyone can also explain why they need to be pointers and why it prints the way it does, that would be immensely helpful.

#include <stdio.h>

int main() {
  char arr[2][2];
  char str[20];
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("%s\n", "please put in a string: ");
      scanf("%s\n", str[0]);
      arr[i][j] = str[0];
    }
  }
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("arr[%d][%d] == %s\n", i,j,arr[i][j]);
    }
  }
  return 0;
}

the output that i got:

please put in a string:
pleasework
what
please put in a string:
s
please put in a string:
f
please put in a string:
g
arr[0][0] == f
arr[0][1] == f
arr[1][0] == f
arr[1][1] == f

Not the right outputs

1

There are 1 best solutions below

4
Duck Dodgers On

If you want to just learn about:

"I'm having a hard time understanding the process for populating a multidimensional array."

then forget the string and the char, and do something simple, like use an int. The following code sample shows exactly that.

Simplest case: Populate multi-dim array of any type

#include <stdio.h>

int main() {
  int arr[2][2];
  int str;
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("please put in a number [%d][%d]: ", i,j);
      scanf("%d", &str);
      arr[i][j] = str;
    }
  }
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("arr[%d][%d] == %d\n", i,j,arr[i][j]);
    }
  }
  return 0;
}
$ ./stackoverflow
please put in an number[0][0]: 1
please put in an number[0][1]: 2
please put in an number[1][0]: 34
please put in an number[1][1]: 450

arr[0][0] == 1
arr[0][1] == 2
arr[1][0] == 34
arr[1][1] == 450

Populate chars

If you want to use single characters, then you can use the following code also, BUT you have to take care. scanf works in mysterious ways. Notice how the space is there before the %c. That is to consume the \n that is there you press enter. You can read up here, but that was the reason for why you had to initially enter twice somet input and press enter. Frankly, I would use something else than scanf like fgets maybe, but since you used scanf, I showed the code which uses it also.

#include <stdio.h>

int main() {
  char arr[2][2];
  char str;
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("%s", "please put in a character: ");
      if (scanf(" %c", &str) == 1)  { arr[i][j] = str; }
    }
  }
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("arr[%d][%d] == %c\n", i,j,arr[i][j]);
    }
  }
  return 0;
}
$ ./stackoverflow
please put in a character: a
please put in a character: b
please put in a character: c
please put in a character: d

arr[0][0] == a
arr[0][1] == b
arr[1][0] == c
arr[1][1] == d

Populate Strings

If you want to read strings, then you need to do it a little differently. You need not a 2 dimensional array. You need a 3-dimensional array because the strings are themselves char-arrays. Without a 3D-array you overwrote the 2D array and only got the last value printed multiple times. What was worse, since your multidimensional array is 2x2 while the input string is 1xn, and where n was greater than 2, you would have gotten buffer overflow. The following code fixes these.

#include <stdio.h>
#include <string.h>

int main() {
  char arr[2][2][20];
  char str[20];
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("%s", "please put in a string: ");
      if (scanf("%s", &str[0]) == 1)
      {
          // arr[i][j] = &str[0];
          strncpy(arr[i][j], str, 20);
      }
    }
  }
  for(int i = 0; i < 2; i++){
    for (int j = 0; j < 2; j++){
      printf("arr[%d][%d] == %s\n", i,j, arr[i][j]);
    }
  }
  return 0;
}
$ ./stackoverflow
please put in a string: ab
please put in a string: cd
please put in a string: ef
please put in a string: gh

arr[0][0] == ab
arr[0][1] == cd
arr[1][0] == ef
arr[1][1] == gh

And just for good measure, here is the 4th variation, which uses char* arr[2][2] instead of a 3D-char array. It doesn't change much, but like I said, just for good measure.

#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main() {
    char* arr[2][2];
    char str[20];
    for(int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
            printf("%s", "please put in a string: ");
            if (scanf("%s", &str[0]) == 1)
            {
                arr[i][j] = (char*)malloc(20*sizeof(char));
                strncpy(arr[i][j], str, 20);
            }
        }
    }

    for(int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
            printf("arr[%d][%d] == %s\n", i,j, arr[i][j]);
        }
    }
  return 0;
}