大家好我有问题
如何将函数中的多维数组传递给main函数,并从那里将它传递给另一个函数。
float array2[][3] = get_sizes(numbers);
编译器说上面的字符串是一个错误,因为getsizes是一个无效的initaliser。 有人可以告诉我们我们做错了什么吗? 提前谢谢。
修改
int main()
{
float anzahl = get_number();
if(feof(stdin))
{
return 0;
}
int anzahl2 = anzahl;
float array2[][3] = get_sizes(anzahl);
if (feof(stdin))
{
return 0;
}
triangulation(array2, anzahl2);
return 0;
}
int get_number(void)
{
printf("Please enter the number of triangles to check: ");
int anzahl = 0;
char check = 0;
int ret = 0;
while(!(ret == 2 && check == '\n'))
{
ret = scanf("%d%c", &anzahl, &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
printf("Please enter the number of triangles to check: ");
my_flush();
}
if((check == '\n' && anzahl > UCHAR_MAX) || (check == '\n' && anzahl <= 0))
{
printf("[ERR] Invalid number of triangles.\n");
printf("Please enter the number of triangles to check: ");
ret = 3;
}
}
return anzahl;
void my_flush(void)
{
while(getchar() != '\n')
{
}
}
int vgl(const void *a, const void *b)
{
int aa, bb;
aa = *(int *)a;
bb = *(int *)b;
return (aa - bb);
float get_sizes(int anzahl)
{
float array [3] = {0,0,0};
float array2[100][3];
while(anzahl>0)
{
char check = 0;
int ret = 0;
while(!(ret == 2 && check == '\n'))
{
printf("Please enter the first number of the triplet: ");
ret = scanf("%f%c",&array[0], &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
my_flush();
}
if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >=
FLT_MAX))
{
printf("[ERR] Invalid number of triangles.\n");
ret = 3;
}
}
check = 0;
ret = 0;
while(!(ret == 2 && check == '\n'))
{
printf("Please enter the second number of the triplet: ");
ret = scanf("%f%c",&array[1], &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
my_flush();
}
if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >=
FLT_MAX))
{
printf("[ERR] Invalid number of triangles.\n");
ret = 3;
}
}
check = 0;
ret = 0;
while(!(ret == 2 && check == '\n'))
{
printf("Please enter the third number of the triplet: ");
ret = scanf("%f%c",&array[2], &check);
if (feof(stdin))
{
return 0;
}
if (check != '\n')
{
printf("[ERR] Invalid number of triangles.\n");
my_flush();
}
if((check == '\n' && array[0] <= 0.0)|| (check == '\n' && array[0] >=
FLT_MAX))
{
printf("[ERR] Invalid number of triangles.\n");
ret = 3;
}
}
qsort(array, 3, sizeof(array[3]), vgl);
array2[anzahl][0]=array[0];
array2[anzahl][1]=array[1];
array2[anzahl][2]=array[2];
anzahl--;
}
return array2;
float triangulation(float array2[100][3], int anzahl2)
{
int number = 1;
while(anzahl2>0)
{
if(array2[anzahl2][0] == 1 && array2[anzahl2][1] == 1 && array2[anzahl2]
[2] == 2)
{
printf("Tripelt %d (a=%f, b=%f, c=%f)is NO triangle.\n", number,
array2[anzahl2][0], array2[anzahl2][1], array2[anzahl2][2]);
}
else
{
printf("Tripelt %d (a=%f, b=%f, c=%f)is a triangle.\n", number,
array2[anzahl2][0], array2[anzahl2][1], array2[anzahl2][2]);
if(array2[anzahl2][0] == array2[anzahl2][1] && array2[anzahl2][0] ==
array2[anzahl2][2] && array2[anzahl2][1]== array2[anzahl2][2])
{
printf("It is an equilateral triangle.\n");
printf("It is a isosceles triangle.\n");
}
else if(array2[anzahl2][0] == array2[anzahl2][1] || array2[anzahl2][0] ==
array2[anzahl2][2] || array2[anzahl2][1]== array2[anzahl2][2])
{
printf("It is a isosceles triangle.\n");
}
else if(((array2[anzahl2][0])*(array2[anzahl2][0])) + ((array2[anzahl2]
[1])*(array2[anzahl2][1])) == ((array2[anzahl2][2])*(array2[anzahl2][2])))
{
printf("It is a right triangle.\n");
}
}
anzahl2--;
number++;
}
}
这是我的整个代码
我们不允许使用全局变量
答案 0 :(得分:0)
这是一个演示程序,显示了一般情况下如何完成。
#include <stdio.h>
#include <stdlib.h>
#define N 3
float ( * get_sizes( size_t n ) )[N]
{
float ( *a )[N] = malloc( n * sizeof( float[N] ) );
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
a[i][j] = i * N + j;
}
}
return a;
}
void display( float( *a )[N], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < N; j++ )
{
printf( "%4.1f ", a[i][j] );
}
putchar( '\n' );
}
}
int main(void)
{
size_t n = 4;
float ( *a )[N] = get_sizes( 4 );
display( a, n );
free( a );
return 0;
}
程序输出
0.0 1.0 2.0
3.0 4.0 5.0
6.0 7.0 8.0
9.0 10.0 11.0
那就是你需要动态分配数组。
答案 1 :(得分:0)
这个简单的代码指示如何将二维数组发送到函数。函数func1
检索float a
和int x
指向的int y
中的值。对于两个以上的维度也应该这样做。
#include <stdio.h>
float func1(float *,int x, int y,int mx, int my);
float func1(float * a,int x, int y,int mx, int my)
{
if (x>=mx)
return 0;
if (y>=my)
return 0;
return a[x*my+y];
}
int main(void)
{
#define _MX 4
#define _MY 3
int x, y;
float a[_MY][_MX] = {
{0.0,0.1,0.2,0.3},
{1.0,1.1,1.2,1.3},
{2.0,2.1,2.2,2.3}
};
for(x=0;x<_MX;x++) {
for(y=0;y<_MY;y++){
// With cast
printf("x =%2d y =%2d => v = %f\n",x,y,func1((float *)a,x,y,_MX,_MY));
}
}
puts("---------------------------------");
for(x=0;x<_MX;x++) {
for(y=0;y<_MY;y++){
// Without cast
printf("x =%2d y =%2d => v = %f\n",x,y,func1(a[0],x,y,_MX,_MY));
}
}
return 0;
}