c中没有malloc的字符串数组

时间:2023-02-05 03:15:19

Can someone tell me whats wrong with that code?And i cant use malloc because i havent learn it in class.I mean can i make a 2d array of strings without malloc and if yes how i am i supposed to write an element when i want to change it/print it/scan it.Thanks in advance

有人能告诉我这个代码有什么问题吗?我不能使用malloc,因为我没有在课堂上学习它。我的意思是我可以制作一个没有malloc的二维数组字符串,如果是的话,我应该如何写我想要的元素更改/打印/扫描它。谢谢

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

2 个解决方案

#1


2  

So, you need a 2d array of strings (char arrays). One way to achieve this would be to allocate a 3d array of char as:

所以,你需要一个2d的字符串数组(char数组)。实现此目的的一种方法是将3d数组分配为:

char x[50][7][MAX_LENGTH];

You can think as having a matrix of array start (of pointers) and then, another dimension to give depth to your matrix (i.e. storage space for your string).

你可以认为有一个数组start(指针)矩阵,然后是另一个维度来给你的矩阵提供深度(即你的字符串的存储空间)。

Your approach is also fine, as long as you are willing to allocate manually using malloc or similar storage space for your strings.

您的方法也很好,只要您愿意使用malloc或类似的存储空间为您的字符串手动分配。

#2


0  

can i make a 2d array of strings without malloc

我可以制作一个没有malloc的二维字符串数组

Sure. Let's reduce this to 2*3:

当然。让我们把它减少到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]);
    }
  }
}

Output:

输出:

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'

#1


2  

So, you need a 2d array of strings (char arrays). One way to achieve this would be to allocate a 3d array of char as:

所以,你需要一个2d的字符串数组(char数组)。实现此目的的一种方法是将3d数组分配为:

char x[50][7][MAX_LENGTH];

You can think as having a matrix of array start (of pointers) and then, another dimension to give depth to your matrix (i.e. storage space for your string).

你可以认为有一个数组start(指针)矩阵,然后是另一个维度来给你的矩阵提供深度(即你的字符串的存储空间)。

Your approach is also fine, as long as you are willing to allocate manually using malloc or similar storage space for your strings.

您的方法也很好,只要您愿意使用malloc或类似的存储空间为您的字符串手动分配。

#2


0  

can i make a 2d array of strings without malloc

我可以制作一个没有malloc的二维字符串数组

Sure. Let's reduce this to 2*3:

当然。让我们把它减少到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]);
    }
  }
}

Output:

输出:

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'