【问题标题】:Problems in three dimension array in CC中三维数组的问题
【发布时间】:2020-05-18 13:52:22
【问题描述】:

我定义了这样的三维数组,但它无法读取其中的任何字符串?哪里有问题?谢谢!

int stuTotal, courseTotal,i,k;//a dynamic array
printf("Input the total number of students that you would like to import:");
scanf("%d", &stuTotal);
printf("Input the total number of courses that you would like to import:");
scanf("%d", &courseTotal);

char ***data = (char***)calloc(stuTotal,sizeof(char));//data
for(i = 0;i < stuTotal;i++){
    data[i] = (char**)calloc(courseTotal,sizeof(char));
    for (k = 0;k < courseTotal;k++){
        data[i][k] = (char*)calloc(20,sizeof(char));
    }
}
strcpy(data[0][0],"hello");

data[0][0] 显示为空。

【问题讨论】:

  • 首先,您真的需要 3D 数组吗?其次,您使用sizeof(char) 进行所有分配。

标签: c arrays string pointers calloc


【解决方案1】:

当您分配外部数组时,sizeof 的参数不正确 - 而不是

char ***data = (char***)calloc(stuTotal,sizeof(char));

应该是

char ***data = (char***)calloc(stuTotal,sizeof(char **)); // you're allocating N elements of type `char **`.

您可以大大简化该调用,如下所示:

char ***data = calloc( stuTotal, sizeof *data ); // sizeof *data == sizeof (char **)

类似

data[i] = calloc( courseTotal, sizeof *data[i] ); // sizeof *data[i] == sizeof (char *)

您不需要转换 malloccalloc 的结果,除非您使用 C++(在这种情况下,您应该使用 new,或者更好的是,某种容器类型)或 C 实现(1989 年之前)。

【讨论】:

    【解决方案2】:

    您应该将 sizeof(char**) 用于 3d“数据”数组,因为该 3d 数组的每个元素都是 char**。

    int stuTotal, courseTotal,i,k;//a dynamic array
    printf("Input the total number of students that you would like to import:");
    scanf("%d", &stuTotal);
    printf("Input the total number of courses that you would like to import:");
    scanf("%d", &courseTotal);
    
    char ***data = (char***)calloc(stuTotal,sizeof(char**));//data
    for(i = 0;i < stuTotal;i++){
        data[i] = (char**)calloc(courseTotal,sizeof(char*));
        for (k = 0;k < courseTotal;k++){
            data[i][k] = (char*)calloc(20,sizeof(char));
        }
    }
    strcpy(data[0][0],"hello");
    

    【讨论】:

      【解决方案3】:

      您指定的已分配对象的大小无效。

      你必须写

      char ***data = (char***)calloc(stuTotal,sizeof(char **));//data
                                                     ^^^^^^^   
      for(i = 0;i < stuTotal;i++){
          data[i] = (char**)calloc(courseTotal,sizeof(char *));
                                                      ^^^^^^ 
          for (k = 0;k < courseTotal;k++){
              data[i][k] = (char*)calloc(20,sizeof(char));
          }
      }
      

      如果您的编译器支持可变长度数组,那么您可以通过调用malloccalloc 只分配一次所需的数组。例如

      char ( *data )[courseTotal][20] = 
          malloc(  sizeof( char[stuTotal][courseTotal][20] ) );
      

      char ( *data )[courseTotal][courceName] = 
          calloc(  1, sizeof( char[stuTotal][courseTotal][courceName] ) );
      

      这是一个演示程序。

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      int main(void) 
      {
          size_t stuTotal = 5, courseTotal = 10, courceName = 20;
      
          char ( *data )[courseTotal][courceName] = 
              calloc(  1, sizeof( char[stuTotal][courseTotal][courceName] ) );
      
      
          strcpy(data[0][0],"hello");
      
          puts( data[0][0] );
      
          free( data );
      
          return 0;
      }
      

      它的输出是

      hello
      

      在这种情况下,要释放所有分配的内存,只需调用一次free

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-15
        • 2021-04-08
        • 2010-09-08
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 2016-05-03
        • 2017-04-11
        相关资源
        最近更新 更多