0

誰かがそのコードの何が問題なのか教えてもらえますか?そして、私はクラスでそれを学んでいないので、mallocを使用できません.つまり、mallocなしで文字列の2次元配列を作成できますか?変更/印刷/スキャンしてください。よろしくお願いします

int main() {

    size_t x,y;
    char *a[50][7];

    for(x=0;x<=SIZEX;x++)
    {
        printf("\nPlease enter the name of the new user\n");
        scanf(" %s",a[x][0]);

        printf("Please enter the surname of the new user\n");
        scanf(" %s",a[x][1]);

        printf("Please enter the Identity Number of the new user\n");
        scanf(" %s",a[x][2]);

        printf("Please enter the year of birth of the new user\n");
        scanf(" %s",a[x][3]);

        printf("Please enter the username of the new user\n");
        scanf(" %s",a[x][4]);

    }

    return 0;
}
4

2 に答える 2

0

malloc なしで文字列の 2 次元配列を作成できますか

もちろん。これを 2*3 に減らしましょう。

#include <stdio.h>

char * pa[2][3] = {
   {"a", "bb","ccc"},
   {"dddd", "eeeee", "ffffff"}
  };

int main(void)
{
  for (size_t i = 0; i < 2; ++i)
  {
    for (size_t j = 0; j < 3; ++j)
    {
      printf("i=%zu, j=%zu: string='%s'\n", i, j, pa[i][j]);
    }
  }
}

出力:

i=0, j=0: string='a'
i=0 j=1: string='bb'
i=0, j=2: string='ccc'
i=1, j=0: string='dddd'
i=1, j=1: string='eeeee'
i=1, j=2: string='ffffff'
于 2016-01-05T18:11:22.493 に答える