c - 将指针的指针传递给函数,同时返回一个 int 和一个地址(通过参数)

标签 c arrays pointers

我有一个功能可以正常工作。但是程序的其余部分有一个限制,因为我已经预设了数组的大小(要分配的空间)。显然,如果出现我需要为该数组提供额外空间的事件,这是有问题的。所以我想在我的程序中添加动态内存分配。

但是我对指向指针概念的整个指针有疑问,而且我完全找不到对我有意义的在线解释...... 我想我会想使用 malloc(iRead + 1) 来获得一个大小合适的数组,但我不确定应该分配给什么……*数组? **大批?我一点也不确定。

我也不清楚我的 while 循环。 &array[iRead] 将不再有效,而且我不确定在涉及指针时如何获取数组中的元素。

谁能给我指出正确的方向(嘿指针双关语)?

最佳答案

我可以想到以下方法。

  1. 第一种方法

    1. 遍历文件两次。
    2. 在第一遍中,读取数字并丢弃它们,但继续计算项目的数量。
    3. 为所有项目分配一次内存。
    4. 倒回文件并再次通过它。在第二遍中,读取并存储数字。

      int getNumberOfItems(FILE* fp, int hexi)
      {
         int numItems = 0;
         int number;
      
         char const* format = (hexi == 0) ? "%X" : "%d";
         while (fscanf(fp, format, &number) > 0) {
            ++numItems;
         return numItems;
      }
      
      void read(int *array, FILE* fp, int numItems, int hexi)
      {
         int i = 0;
         char const* format = (hexi == 0) ? "%X" : "%d";
         for ( i = 0; i < numItems; ++i )
            fscanf(fp, format, &array[i]);
      }
      
      int main(int argc, char** argv)
      {
         int hexi = 0;
         FILE* fp = fopen(argv[1], "r");
      
         // if ( fp == NULL )
         // Add error checking code
      
         // Get the number of items in the file.
         int numItems = getNumberOfItems(fp, hexi);
      
         // Allocate memory for the items.
         int* array = malloc(sizeof(int)*numItems);
      
         // Rewind the file before reading the data
         frewind(fp);
      
         // Read the data.
         read(array, fp, numItems, hexi);
      
         // Use the data
         // ...
         // ...
      
         // Dealloate memory
         free(array);
      }
      
  2. 第二种方法。

    1. 继续从文件中读取数字。
    2. 每次读取数字时,使用realloc 为附加项分配空间。
    3. 将 存储在重新分配的内存中。

      int read(int **array, char* fpin, int hexi)
      {
         int number;
         int iRead = 0;
      
         // Local variable for ease of use.
         int* arr = NULL;
         char const* format = (hexi == 0) ? "%X" : "%d";
      
         FILE *fp = fopen(fpin, "r");
         if (NULL == fp){
            printf("File open error!\n");
            exit(-1);
         }
      
         while (fscanf(fp, format, &number) > 0) {
            arr = realloc(arr, sizeof(int)*(iRead+1));
            arr[iRead] = number;
            iRead += 1;
         }
      
         fclose(fp);
      
         // Return the array in the output argument.
         *array = arr;
      
         return iRead;
      }
      
      int main(int argc, char** argv)
      {
         int hexi = 0;
         int* array = NULL;
      
         // Read the data.
         int numItems = read(&array, argv[1], hexi);
      
         // Use the data
         // ...
         // ...
      
         // Dealloate memory
         free(array);
      }
      

关于c - 将指针的指针传递给函数,同时返回一个 int 和一个地址(通过参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26617008/

相关文章:

c++ - 将非模板函数指针传递给模板方法

c - 赋值从指针生成整数而不进行强制转换 [-Werror]

c++ - 如何在U盘中制作安装程序?

c++ - 将 malloc 转换为 new

python - 如何将 3 个列表转换为 1 个 3D Numpy 数组

python - 具有多个条件的数组索引操作

javascript - 如果填写字段=数组则为JAVASCRIPT

c - malloc 在这种情况下做了什么?

c - 回调中函数指针的基本原理

c - 一些带有 'extern' 指针的神秘主义者,指向 C 中的结构