相关疑难解决方法(0)

双指针实现二维数组实现

请考虑以下代码:

#include <stdio.h>
#include <stdlib.h>

#define NUM_ARRAYS     4
#define NUM_ELEMENTS   4
#define INVALID_VAL   -1

int main()
{
   int index            = INVALID_VAL;
   int array_index      = INVALID_VAL;
   int **ptr            = NULL;

   ptr = malloc(sizeof(int*)*NUM_ARRAYS);

   if (!ptr)
   {
      printf ("\nMemory Allocation Failure !\n\n");
      exit (EXIT_FAILURE);
   }

   for (index=0; index<NUM_ARRAYS; index++)
   {
      *(ptr+index) = malloc(sizeof(int)*NUM_ELEMENTS); 

      if (!*(ptr+index))
      {
         printf ("\nMemory Allocation Failure !\n");
         exit (EXIT_FAILURE);
      }
   }

   /* Fill Elements Into This 2-D Array */
   for (index=0; index<NUM_ARRAYS; index++)
   {
      for (array_index = 0; …
Run Code Online (Sandbox Code Playgroud)

c double-pointer multidimensional-array

8
推荐指数
2
解决办法
5万
查看次数

标签 统计

c ×1

double-pointer ×1

multidimensional-array ×1