C语言 将双指针(动态分配的双数组)传递给函数


double** makeit(int, int);
void showit(double**, int, int, int);
int main()
{
    int i,j;
    int x,y;
    printf("x=");
    scanf("%d",&x);
    printf("y=");
    scanf("%d",&y);
    double (*mas2d)[x];
    mas2d=makeit(x,y);
    printf("%4.0f ir  %4.0f n",mas2d[0][0],mas2d[1][0]);
    showit(&mas2d, x, y);
    return 0;
}
double** makeit(int x, int y)
{
    double (*masp)[x];
    int i,j;
    double skc;
    masp= malloc((x*y)*sizeof(double));
    skc=1;
    for (i=0;i<x;i++)
    {
        for (j=0;j<y;j++)
        {
            masp[i][j]=skc;
            skc++;
        }
    }
    return masp;
}
void showit(double** mas[], int x, int y)
{
    int i,j;
    printf("%4.0f ir  %4.0f n",mas[0][0],mas[1][0]);
    printf("x===%d",x);
    for(i=0;i<x;i++)
    {
        printf("n");
        for(j=0;j<y;j++) 
        {
            printf("%4.0f n",mas[i][j]);
        }
    }
}

我做什么1.我在函数makeit中动态分配双数组mas2d。2.我想将该mas2d数组指针发送到函数showit并在那里打印。

怎么了我可以毫无问题地从main函数打印mas2d数组指针,但是当我将其传递给单独的函数showit时,我就是无法让它工作......我一直在尝试将其作为 3D 指针发送,也许还有其他 100 种方式,但根本没有运气。

执行此操作的最

干净方法是让 2D 的所有实例都使用 double ** 类型。 当您进行分配时,您首先需要分配一个 x double * 数组,然后为每一行分配y double

double** makeit(int x, int y){
    double **masp;
    int i,j;
    double skc;
    masp= malloc(x*sizeof(double *));
    skc=1;
    for (i=0;i<x;i++) {
        masp[i]= malloc(y*sizeof(double));
        for (j=0;j<y;j++){
            masp[i][j]=skc;
            skc++;
        }
    }
    return masp;
}

试试这个(请参阅有关更改的详细信息作为对源的注释):

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
double (*makeit(size_t x, size_t y))[] /* Make it return what it creates,
                                                      a pointer to an array of double.  */
{
  double (*masp)[x];
  size_t i, j; /* Index arrays and address storage using size_t. */
  double skc = 1.; /* Always initialise on definition if possible; Use a double literal 
                                                                to initialise a double. */
  masp = malloc(y * sizeof *masp); /* Allocate y times what masp points to,
                                                         that is an array of x doubles. */
  if (NULL == masp) /* Return is we got nothing. */
  {
    return NULL;
  }
  /* skc = 1; */ /* Not needed any more (see definition of skc). */
  for (i = 0; i < x; i++)
  {
    for (j = 0; j < y; j++)
    {
        masp[i][j] = skc;
        skc++;
    }
  }
  return masp;
}
void showit(size_t x, size_t y, double (*mas)[x]) /* Pass the address of a VLA
                                              after passing it primary dimension (x). */
{
  size_t i, j; /* Index arrays and address storage using size_t. */
  printf("%4.0f ir  %4.0f n",mas[0][0], mas[1][0]);
  printf("x===%zu", x); /* Use the appropriate conversion specifier for size_t */
  for (i = 0; i < x; i++)  
  {
    printf("n");
    for (j = 0; j < y; j++)
    {
        printf("%4.0f n", mas[i][j]);
    }
  }
}
int main(void) /* It ought to be this at least. */
{
  //int i, j; /* Unused. */
  size_t x,y; /* Index array and address storage using size_t. */
  printf("x=");
  fflush(stdout); /* Make sure the output is shown. */
  scanf("%zu",&x); /* Use the appropriate specifier for size_t */
  printf("y=");  /* Make sure the output is shown. */
  fflush(stdout); /* Use the appropriate specifier for size_t */
  scanf("%zu",&y); /* Make sure the output is shown. */
  double (*mas2d)[x] = makeit(x, y); /* Always initialise on definition if possible. */
  if (NULL == mas2d) /* Do error checking! */
  {
    perror("makeit() failed");
    exit(EXIT_FAILURE);
  } 
  printf("%4.0f ir  %4.0f n", mas2d[0][0], mas2d[1][0]); 
  showit(x, y, mas2d); /* Adjust order of parameters suiting the changes. */
  return 0;
}

最新更新