2

I have an issue with structures in C, I don't know exactly what is happening. Could you explain me where I am wrong and correct me?

I declared a structure like below

typedef struct  
{
    Fuel_Intrl_IDs Curr_Bar;     /*enum variable*/
    uint16_t Pres_Value; 
    uint16_t Prev_Value;
}Bar_Dync_Data;

I assigned values for all the 3 variables, then when I am accessing data in these variables, value in "Prev_value is always returning "0".

On reassigning the structure as follows, everything is working fine:

typedef struct  
{
    uint16_t Pres_Value; 
    Fuel_Intrl_IDs Curr_Bar;     /*enum variable*/
    uint16_t Prev_Value;
}Bar_Dync_Data;

Can you explain what is happening here?

Code for reproducing

#include <stdio.h>
#include <stdlib.h>
#include <stdint-gcc.h>

typedef enum                                                            /*Fuel Internal IDs*/
{
   Fuel_Intrl_ID_End
}Fuel_Intrl_IDs;

typedef struct                                                          /*Structure for Storing BAR ON/OFF Values*/
{
    Fuel_Intrl_IDs Curr_Bar;
    uint16_t Pres_Value;
    uint16_t Prev_Value;
}Bar_Dync_Data;

 Bar_Dync_Data FBar_Dync_Data;

int main()
{
    printf("Hello world!\n");
    FBar_Dync_Data.Prev_Value = 255;
        while(1)
    {
        printf("\n Enter Pres value ");
        scanf("%d",&FBar_Dync_Data.Pres_Value);
        printf("\n Present value:  %d",FBar_Dync_Data.Pres_Value);
        printf("\n Previous value:  %d",FBar_Dync_Data.Prev_Value);
        FBar_Dync_Data.Prev_Value = FBar_Dync_Data.Pres_Value;
       // getch();
    }
    return 0;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    not related to the question itself but `Prev_Value` and `Pres_Value` are not easy to distinguish, what about `current` and `previous` ? not sure `_Value` is bringing a lot – Ôrel Nov 24 '22 at 07:40
  • 1
    Most likely you have a buffer overflow in some code unrelated to the structures. – Barmar Nov 24 '22 at 07:56
  • 3
    The `%d` specifier in `scanf` is for the `int` type. You use it on an `uint16_t` which results in undefined behaviour – Jabberwocky Nov 24 '22 at 11:27
  • 1
    Once code attempts to execute the `scanf` my system traps a non-fatal run-time error: _"Parameter type mismatch; expecting pointer to int but found pointer to short"_. And, as noted, behavior beyond that point is undefined. Anything can happen. Either change type of member `Pres_Value` to `int`, or use the `hu` format specifier in the `scanf` call. – ryyker Nov 28 '22 at 13:05
  • Also, upon first use of `Prev_Value` it has not yet been initialized. Suggest initializing `struct` before using: `Bar_Dync_Data FBar_Dync_Data = {0};`. ([more on that](https://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default)) – ryyker Nov 28 '22 at 13:13
  • If you have not yet encountered it in your studies, read about [undefined behavior (UB) here](https://en.wikipedia.org/wiki/Undefined_behavior). As to the different behavior you are observing upon changing member order within struct, it could result from the combined effects of UB and [struct padding](https://stackoverflow.com/questions/4306186/structure-padding-and-packing), Arranged one way, there may appear to be no impact from memory violations, but arranged another way the violation presents itself, as (in my case) a non-fatal run-time error. – ryyker Nov 28 '22 at 13:22

0 Answers0