[英]Printf not showing and return value 3221225477
New to C and programming in general, so I am having kind of a tough time with structs when combined with arrays and pointers. C和程序设计的新手,因此在与数组和指针结合使用时,对结构来说我遇到了一段艰难的时期。 I'm trying to create a struct with attempts, then create an array pointer (towards the struct) repeating it 10 times. 我试图尝试创建一个结构,然后创建一个数组指针(指向该结构)重复10次。 Then find the average for every struct and print it. 然后找到每个结构的平均值并打印出来。
Everything seems to work normally providing a return value of 0 until the loop. 在循环之前,一切似乎都正常工作,提供的返回值为0。
#include <stdio.h>
#include <stdlib.h>
typedef struct Tries {
float attempts1;
float attempts2;
float attempts3;
float aver;
}Try;
int main(int argc, char *argv[]) {
int i,size=10,at1,at2,at3;
Try** arrayofTries= malloc (sizeof(Try)*size);
for (i=0;i<size;i++){
arrayofTries[i]->attempts1= rand () %(900 - 700)+700;
arrayofTries[i]->attempts2= rand () %(900 - 700)+700;
arrayofTries[i]->attempts3= rand () %(900 - 700)+700;
at1= arrayofTries[i]->attempts1;
at2= arrayofTries[i]->attempts2;
at3=arrayofTries[i]->attempts3;
arrayofTries[i]->aver = (at1+at2+at3)/3;
printf ("The average of %d person is%f",i,arrayofTries[i]->aver);
}
return 0;
}
arrayofTries
should be of type Try *
not Try **
arrayofTries
的类型应为Try *
not Try **
Try *arrayofTries = malloc (sizeof(Try) * size);
So all your ->
should be simple dots .
因此,所有->
应该都是简单的点.
. 。
You are allocating a pointer to pointer to Try
but not allocating the direct pointer to the elements themselves. 您正在分配一个指向Try
指针,但没有分配到元素本身的直接指针。 It looks like your intent is to declare a 1D array, not a 2D array, so what you should do is change the type of arrayofTries
from Try **
to Try *
. 看来您的意图是声明一个1D数组,而不是2D数组,因此您应该做的是将arrayofTries
的类型从Try **
更改为Try *
。
Also, when you're done using memory allocated with malloc
, free
it: 另外,当您完成使用malloc
分配的malloc
,请free
它:
free(arrayofTries);
arrayofTries = NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.