1

new to code. Currently doing a project which I'll need to use a array of structs. I have something like this as a sample:

#include <stdio.h>

  struct InfoCar
   {
     int OnOff;
     char CarNumber[9];
   }CarData[10];    

int main(void) 
{

int i;

    for(i=0;i<10;i++)
    { 
      printf("%d", CarData[i].OnOff); 
    }


    return 0;
}

The thing is, from what I've learned and searched all int without value would store a random value, but this one is showing all output CarData[i].OnOff to be 0. Am I wrong or is this due to the struct array ?

Dan's
  • 21
  • 4
  • 1
    Besides your misconception about initialization, "all values 0" is one possible version of "holds random values". It can also hold "all values 0" only during first call but something else on later calls. If the array wasn't initialized as global data, reading the values would be illegal anyway and causing undefined behaviour. And undefined behaviour also includes to produce the expected result just by accident. – Gerhardh Jan 09 '22 at 00:16

0 Answers0