从结构中获取信息并在另一个函数中使用

时间:2016-03-20 03:23:00

标签: c function structure

我需要知道我是否可以在结构中调用tire_pressure中的信息并在另一个函数中使用它?基本上我不知道如何将它从一个调用到另一个。我是否必须在tireTest中声明该功能?这是我试图访问tire_pressure信息的功能。谢谢你的帮助

#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

}

1 个答案:

答案 0 :(得分:0)

我认为你的问题比现在更困难 - 看看这种简化是否能解决你的问题:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define PRESSURE_MIN 18
#define PRESSURE_MAX 35

struct tires
{
    char Manufacturer[40];
    int tire_pressure;
    int pressure_change;
} typedef tires;

// Prototypes
void getTireInformation(tires *, int);
void tirePressure(tires *, int);
void tireTest(tires *, int);

//===============================================================

void getTireInformation(tires *ptire, int size)
{
    for (int i = 0; i < size; i++)
    {
        printf("please enter Make for the tire: \n");
        scanf("%s", ptire[i].Manufacturer);
    }

    printf("all the tire makes you entered ... just for verification:\n");

    for (int i = 0; i < size; i++)
    {
        printf("%s\n", ptire[i].Manufacturer);
    }

} // end getTireInformation

//===============================================================

void tirePressure(tires *ptire, int size)
{
    for (int i = 0; i < size; i++)
    {
        ptire[i].tire_pressure = rand() % (PRESSURE_MAX - PRESSURE_MIN + 1) + PRESSURE_MIN;
        printf("The Tire pressure is: ");
        printf("%d\n", ptire[i].tire_pressure);
    } // end for
} // end tirePressure

//==============================================================

void tireTest(tires *ptire, int size)
{
    int min = 2;
    int max = 5;

    for (int i = 0; i < size; i++)
    {
        int change = rand() % (max - min + 1) + min;

        printf("Pressure change from test %d: %d\n", i + 1, change);

        ptire[i].pressure_change = change + ptire[i].tire_pressure;

        printf("%d\n", ptire[i].pressure_change);
    } // end for
}

//===============================================================

int main()
{
    tires tire[4];

    srand(time(NULL));

    getTireInformation(tire, 4);
    tirePressure(tire, 4);
    tireTest(tire, 4);

    return 0;
} // end main