[英]Structures and pointers in C code
What asterisk ' *
' doing in int function before data variable?What is the aim of putting pointer of type struct weather in brackets?Also why before rain is ' -->
'sign?How it works in this code?数据变量之前的int函数中的星号'
*
'做什么?将struct weather类型的指针放在括号中的目的是什么?还有为什么在下雨之前是' -->
'符号?它在这段代码中是如何工作的? Thank you in advance!先感谢您!
typedef struct
{
int rain;
double temp;
double wind;
} weather;
int rainTot(weather* data)
{
int i, total = 0;
for (i = 0; i < 12; i++)
{
total += (data + i)->rain;
}
return total;
}
btw code for main顺便说一句主代码
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
int i = 0;
weather *values = (weather*)malloc(12 * sizeof(weather));
FILE *data;
data = fopen("astana.txt", "r");
if (data == NULL)
{
printf("Problem opening files.");
return 1;
}
do
{
fscanf(data, "%i %lf %lf", &(values + i)->rain, &(values + i)->temp, &(values + i)->wind);
i++;
}
while (!feof(data));
printf("The total rainfall for one year is: %i\n", rainTot(values));
printf("The lowest average temperature is: %g\n", tempAvgMin(values));
printf("The average of wind speed for one year is %.2g\n\n", windAvg(values));
print(values);
free(values);
fclose(data);
return 0;
}
Asterisk *
means this variable is a pointer.星号
*
表示这个变量是一个指针。
Pointer in paramaters usually has two reason : 1. You nead modify paramaters in sub-function 2. Paramaters are to many,so transfer paramters' address(pointer) to function to simplify。参数中的指针通常有两个原因:1.您需要修改子函数中的参数 2.参数太多,因此将参数的地址(指针)传递给函数以简化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.