Home > Software engineering >  C: string and struct to function does not work
C: string and struct to function does not work

Time:08-31

I have a predefined structure here from a code that I am not allowed to adapt. I now need to create the temperature calculations in the function calculate_temperatures.

My problem is that I have already tested several ways of passing the parameters in the function. But none of them worked.

struct temperature {
    float *data;
    int size;
} temperatures;

struct temperature get_temperature_Data() {
    temperatures.size = 0;
    temperatures.data = (float *)malloc(sizeof(float));
    temperatures.data[temperatures.size  ] = 25.9;
    temperatures.data[temperatures.size  ] = 55.2;
    return temperatures;
}

void calculate_temperatures(temperatures, testmessage) {
    //test output for correct data
    printf("%s\n", testmessage);
    printf("test %d\n", temperatures.size);
}

int main() {
    int i;
    temperatures = get_temperature_Data();
    calculate_temperatures(temperatures, "Testoutput");
}

Two of my trys:

void calculate_temperatures(temperatures, char *testmessage) {
void calculate_temperatures(temperatures, char testmessage[]) {

CodePudding user response:

gcc tells you what is the problem:

warning: type of ‘temperatures’ defaults to ‘int’ [-Wimplicit-int]
    void calculate_temperatures(temperatures, testmessage) {

It should be

void calculate_temperatures(struct temperature temperatures, const char *testmessage) {

Warning, your get_temperature_Data is wrong: not enough memory is allocated.

  •  Tags:  
  • c
  • Related