What is the difference between the values of these two char arrays?

Sp1cyChef

I'm working on code in C that reads the temperature and sends it to a GUI made with GTK3.

Here is the code:

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>

char Vo[10];         // voltage out
float Ro, R1 = 6247; // value of resistance at 20C in the thermistor
float T0 = 298.15;   // 25 degrees C in Kelvin
float logR2, R2, T;
float A = 1.281426510e-03, B = 2.368116050e-04, C = 0.9002008458e-07;  // Steinhart-Hart and Hart Coefficients

char *senseTemp() {
    FILE *fp;
    char command[50];
    int c;

    //get the Analog value
    fp = fopen("/sys/bus/iio/devices/iio:device0/in_voltage0_raw", "r");
    if (fp == NULL) {
        perror("Error: ");
        return (1);
    }
    int i = 0;
    while (1) {
        c = fgetc(fp);
        if (feof(fp)) {
            break;
        }
        printf("%c ", c);
        if (c != EOF) {
            Vo[i] = c;
            ++i;
        }
    }

    fclose(fp);
    
    //printf("Analog Reading: %s\n", Vo);
    
    //convert the value to resistance
    int V = 0;
    float Vout = 0;
    V = atoi(Vo);           //TO convert an array to an integer
    //printf("Value of V: %d\n", V);
    
    Vout = V * (1.8 / 4095.0);      //Voltage out of the thermistor
    //printf("Voltage out from thermistor: %f\n", Vout);
    
    R2 = R1 * ((1.8 / Vout) - 1);
    
    logR2 = log(R2);
    T = (1.0 / (A + B * logR2 + C * logR2 * logR2 * logR2));  // Steinhart and Hart Equation. T  = 1 / {A + B[ln(R)] + C[ln(R)]^3}
    
    T =  T - 273.15;
    
    char Tfinal[10];
    i = 0;
    
    snprintf(Tfinal, 10, "%.0f", T);
    printf("Here is the value: %s\n", Tfinal);
    
    return Tfinal;
}

If I use return Vo; the values are properly returned to the GUI and I can see them, but I want to convert the values to degrees celsius and that is what the variable T holds. But the gtk_label_set_text() that displays the temperature in the GUI only takes a pointer to a string.

So in my code if I use return Vo it works but not with return Tfinal.

chqrlie

return Tfinal; has undefined behavior because you return the address of an array with local automatic storage. This array is discarded upon funtion return. Allocate the array with malloc() to return it to the caller.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between printing char and int arrays in Java

What is the difference between these two methods

Typescript difference between two arrays

What is the difference between char * const and const char *?

What is the difference between char s[] and char *s?

Finding Difference Between Values in Two JavaScript Arrays of Objects

Difference quantity between two arrays values

What is the difference between these two approaches?

Find difference between two arrays

Difference between two Sorted Arrays

What is the difference between these two loops?

What is the difference between this two lines of code? ML arrays

What is the difference between 'char *' and 'char (*) [100]'?

Passing arrays to functions, what is the difference between these two approaches?

What is the difference between char[] and char?

Incompatible Types in Assignment Between Two Char Arrays

What is the difference between char array[] and char *array?

What is the difference between these two SQL?

What's the Difference between the two of writings of arrays in typescript

What is the difference between these two syntax?

What is the difference between two 'char*' castings in c++

Find difference between two values?

Adding values of two char arrays

What is the difference between these 2 arrays

What is the difference between `char **argv` and `char** argv`?

What is the difference between these two syntaxes

what is the difference between const char * and const char ()[]

difference between two arrays in python

What is difference between these two declarations of associative arrays in Bash?