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.我在函数mas2d
中动态分配双数组makeit
。
2.我想将mas2d
数组指针发送到函数showit
并在那里打印。
问题是什么
我可以毫无问题地从mas2d
函数打印main
数组指针,但是当我将它传递给单独的函数showit
时,我无法让它工作......
我一直试图将它作为3D指针发送,也许还有其他100种方式,根本没有运气。
答案 0 :(得分:1)
最简洁的方法是让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;
}
答案 1 :(得分:0)
尝试此操作(有关更改的详细信息,请参阅源代码):
#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;
}