0

How c_data[4] works here and when i put a for loop to iterate the c_data why it gives me 0 0 1 1

#include<iostream>

using namespace std;

union Data{
    int i_data;
    char c_data[4];
};
    
int main(){
    Data d;
    d.i_data = 257;
    for(int i=3; i >= 0; i--) 
    {
        cout<< (int) d.c_data[i]<<" ";
    }

    cout<< endl;
    cout<< d.i_data;
    return 0;
}
AmrDeveloper
  • 3,826
  • 1
  • 21
  • 30
  • 3
    What were you expecting and why? – Mat Aug 08 '21 at 07:22
  • 2
    Because 257 is 0x00000101 and your processor uses little endian, i.e. lowest-value bytes are stored first (in memory it looks like 01 01 00 00), so iterating over it backwards you get 0x00 0x00 0x01 0x01 i.e. 0 0 1 1. – CherryDT Aug 08 '21 at 07:25
  • 1
    @CherryDT, But, I don't see where ```c_data``` is assigned or initialized with any value ? I only see that ```i_data``` is set to 257. – Job_September_2020 Aug 08 '21 at 07:34
  • @Mat i was expecting 0 0 0 0 because of 257 – saurabh singh Aug 08 '21 at 07:35
  • 1
    You use `union` and not `struct`, so `i_data` and `c_data` occupy the **same memory**. They are two ways to interpret the **same data**. You assign `c_data` by assigning `i_data`. Works the other way round too by the way. (Also, to get 0 0 0 0 you'd have to assign 0 to `i_data`.) – CherryDT Aug 08 '21 at 07:36
  • @CherryDt can you pls tell me how to get value after 0x00 0x00 0x01 and 0x01 are you taking the highest memory from it so the answer will be 0 0 1 1 in last two the highest is 1 is that so??? – saurabh singh Aug 08 '21 at 07:38
  • 5
    Writing to one member of union and then reading from another [is UB](https://stackoverflow.com/questions/10271929/union-for-uint32-t-and-uint8-t4-undefined-behavior) in C++. – Evg Aug 08 '21 at 07:38
  • 1
    Sorry I don't understand the question, what do you mean by saying "the value _after_ 0 0 1 1"? – CherryDT Aug 08 '21 at 07:39
  • I mean how did you get 0 0 1 1 from 0x00 0x00 0x01 0x01 are you taking the highest value – saurabh singh Aug 08 '21 at 07:42
  • 1
    (As Evg said, in C++ this is actually undefined behavior even though it usually works, while in C it is valid to do that. Nonetheless I think it's good for you figure out why this happens to understand what a union really is.) – CherryDT Aug 08 '21 at 07:43
  • 2
    Let's use a different number to make it clearer what's going on. Say we have 2500. In hex that is 0x9C4 (with leading zeroes 0x000009C4). In little endian architectures this is stored in reverse byte order in memory so it's stored as C4 09 00 00 instead of 00 00 09 C4. If you access this as if it were a char array, you'd get 0xC4 = 196 at index [0], 0x09 = 9 at [1] and then two zeroes. So if you'd iterate from index 0 to 3, you'd see (in decimal) 196 9 0 0. Iterating backwards from 3 to 0 like you do here, you'd see 0 0 9 196. (Also, 0*256^3 + 0*256^2 + 9*256^1 + 196*256^0 = 2500 - checks out) – CherryDT Aug 08 '21 at 07:47
  • Thank you @CherryDt i was confused why you reversed it now i get it thanks mate – saurabh singh Aug 08 '21 at 07:52

0 Answers0