0

I am currently learning C and am facing some issues with the codes below:

typedef struct{
    string name;
    int age;
    bool tall;
}
person;

max_index =3;

person x_1[max_index];

int main(void)
{
    //lines of code
    while (true)
    {
     function1();
     function2();
     }
}

int function1(void)
{
   person x_2[max_index];
   for (int j=0; j< max_index; j++)
   {
    if (some conditions)
    {
     x_2[j].name = x_1[j].name;
     x_2[j].age = x_1[j].age;
    } 
   }

}
int function2(void)
{
  person x_3[max_index];
  for (int j=0; j< max_index; j++)
  {
   if (some conditions)
    {
      x_3[j].name = x_1[j].name;
      x_3[j].age = x_1[j].age;
    }
   }
}



  1. person struct
  2. a main function that calls function1 and function2 while evaluating certain conditions
  3. function1 and function2 initialize a new person struct separately - x_2 and x_3

Issue: realise that if the main function calls function1 and function2 > once, the values in x_2 and x_3 remains the same as the previous call. My understanding is since x_2 and x_3 exists only within (local) function1 and function2, each call to both the function will "reset" x_2 and x_3? i.e. a new x_2 and x_3 with each call. But that doesn't seems like the case... I am new to C and not sure what exactly is the issue (unable to google)

Update: indeed, printf helped. There were no issues with the code but rather the debugger was showing wrong variable values.

judebox
  • 59
  • 2
  • 12
  • 1
    *But that doesn't seems like the case* - how did you conclude that? BTW, `function1;` is not how you call functions in C. It's `function1();`. – Eugene Sh. Apr 07 '21 at 17:11
  • realised it when using debugger. The first loop, x_2 and x_3 are "new" - no values for the attributes defined but from the second loop onwards, x_2 and x_3 seems to have the same values as the first loop. EDIT: thanks for highlighting. I have edited the codes to incorporate more information, I might be missing something here... – judebox Apr 07 '21 at 17:18
  • What you have posted is not valid C code (and shouldn't even compile). `x_1`, `x_2` and `x_3` are arrays, but you are accessing them as if they were a single object – UnholySheep Apr 07 '21 at 17:21
  • sorry, my bad. Will edit the codes – judebox Apr 07 '21 at 17:22
  • 3
    You need to post [mcve]. Not a "similar" code, but exactly the same. – Eugene Sh. Apr 07 '21 at 17:23
  • thanks, is it better now? – judebox Apr 07 '21 at 17:27
  • 1
    This is not [mcve]. `(some conditions)` is not compilable. Nevertheless, where do you check th values and what do you see? – Eugene Sh. Apr 07 '21 at 17:27
  • ok, will post the codes but they are kinda lengthy. I am actually learning C via CS50 and they have a debug50 that allows breakpoint and via hovering the mouse over the variable, i can see the values – judebox Apr 07 '21 at 17:29
  • 1
    "the values in x_2 and x_3 remains the same as the previous call". Why do you think so? Please post a *compilable* program that runs and prints something. Then we can look at the output and try to explain it. Try to come up with a program that, [when compiled at the highest warning level, produces no warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – n. m. could be an AI Apr 07 '21 at 17:33
  • 1
    To what @n.'pronouns'm. said - if you want to examine the values, add some output. Debugger watches are showing the values in memory locations, but these are not necessarily valid or well defined. – Eugene Sh. Apr 07 '21 at 17:35
  • thanks everyone. Will examine the values via printing first (had totally forgotten about doing so). If that doesn't help, will update the post with a compilable version. – judebox Apr 07 '21 at 17:59

1 Answers1

2

Variables declared at block scope, such as variables local to a function, take on indeterminate values if they are not explicitly initialized. They are not "reset". That means you can't rely on them having any particular value, and the value being read can even change from one access to the next.

In addition to this, in many cases simply reading a variable whose value is indeterminate can trigger undefined behavior in your program.

dbush
  • 205,898
  • 23
  • 218
  • 273