嗨我想要傳遞一個結構數組,而不是打印它的值,但是當我嘗試它只打印零時,請注意主要工作得很好問題是在最大方法中:無法打印傳遞的結構
#include <stdio.h>
#include <stdlib.h>
struct rectangle {
float length;
float width;
};
void max (struct rectangle * a, int size){
printf("the test %d \n", (*a).width);
int i;
for(i=0; i<size; i++){
printf("the test %d \n", a[i].width);
}
}
void main()
{
printf("enter the size of both arrays \n");
int x ;
scanf("%d",&x);
struct rectangle* r ;
r = malloc(x*sizeof(struct rectangle));
int j ;
for (j = 0 ; j < x ; j++){
printf("enter rectangle length \n");
scanf("%f",&r[j].length);
printf("enter rectangle width \n");
scanf("%f",&r[j].width);
}
for (j = 0 ; j < x ; j++){
printf(" %f\n",r[j].length);
printf(" %f\n",r[j].width);
}
max(r,x)
}
但是當我嘗試運行它崩潰。
'a = a + i'會導致問題,因爲您會轉移太多。將其更改爲'a ++':) – tkellehe
建議:如果您使用數組下標,而不是'** a',則會更容易理解和維護:例如'a [i] - > width'。 – kaylum
完全取出'a = a + i;'並將'(** a)'改爲'(* a)[i]' –