【问题标题】:taking information from a structure and using in another function [closed]从结构中获取信息并在另一个功能中使用[关闭]
【发布时间】: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) -&gt;tire_pressure[0]change[0],因为数组只有零个元素。注意:标准不支持创建元素为零的数组。
  • @MikeCAT 所以如果数组有 2 个元素会起作用吗?
  • 您通过传递指向具有错误数据类型的对象的指针调用了 取消定义行为%s 调用 char*,但您在 @987654327 中传递了 char (*)[40] @
  • 什么是“工作”?该代码可能无法正常运行,因为tireTest() 中有一个未使用的参数ptire

标签: c function structure


【解决方案1】:

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

#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

【讨论】:

  • @cdiane 我什至没有想过让 int min 和 int max 成为常数。我想我已经在这方面工作了很长时间,我开始想太多了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多