【发布时间】:2016-03-20 03:23:00
【问题描述】:
我想知道我是否可以调用结构中的轮胎压力信息并在另一个函数中使用它?基本上我不知道如何从一个到另一个调用它。我必须在轮胎测试中声明函数吗?这是我试图访问轮胎压力信息的功能。感谢您的帮助
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct tires
{
char Manufacturer[40];
int tire_pressure[0];
int pressure_change[0];
}typedef tires;
// Prototypes
void getTireInformation(tires*, int);
void tirePressure(tires*, int);
void tireTest(tires*, int);
int main()
{
tires tire[4];
tires* ptire = &tire[0];
srand(time(NULL));
getTireInformation(ptire, 4);
tirePressure(ptire, 4);
tireTest(ptire, 4);
return 0;
}// end main
//============================
void getTireInformation(tires* ptire, int size)
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("please enter Make for the tire: \n");
scanf("%s", &(ptire + i) ->Manufacturer);
}
printf("all tire make you entered ...just for verification:\n");
for(i = 0; i < size; i++)
printf("%s\n",(ptire +i) ->Manufacturer);
}//end getTireInformation
//=======================================================================
void tirePressure(tires* ptire, int size)
{
int i = 0;
int min = 18;
int max = 35;
for (i = 0; i < size; i++)
{
(ptire + i) ->tire_pressure[0] = rand() % (max - min + 1) + min;
printf("The Tire pressure is: ");
printf("%d\n",(ptire + i) -> tire_pressure[0]);
}// end for
}// end tirePressure
//==============================================================
void tireTest(tires* ptire, int size)
{
int i = 0;
int min = 2;
int max = 5;
int change[0] = {0};
i++;
for (i = 0; i < size; i++)
{
change[0] = rand() % (max - min + 1) + min;
//(ptire + i) -> pressure_change[0] = rand() % (max - min + 1) + min;
printf("Pressure change from test %d: %d\n",i + 1, change[0]);
//(ptire + i) -> pressure_change[0] = change[0] + tirePressure;
//printf("%d\n", (ptire +i) -> pressure_change);
}// end for
}
【问题讨论】:
-
您不能访问
(ptire + i) ->tire_pressure[0]和change[0],因为数组只有零个元素。注意:标准不支持创建元素为零的数组。 -
@MikeCAT 所以如果数组有 2 个元素会起作用吗?
-
您通过传递指向具有错误数据类型的对象的指针调用了 取消定义行为:
%s调用char*,但您在 @987654327 中传递了char (*)[40]@ -
什么是“工作”?该代码可能无法正常运行,因为
tireTest()中有一个未使用的参数ptire。