Thread: Please explain to me why I am having this output

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    4

    Please explain to me why I am having this output

    Hello I am the following piece of code that I am testing and cannot understand the output I'm getting:

    Code:
    #include<stdio.h>
    
    //taken out of library I'm using to test my code
    typedef struct
    {
        unsigned int id;
        struct {
            unsigned char rtr;
            unsigned char ide;
            unsigned char length
        } header;
        unsigned char data[8];
    } tCAN;
    //--------------------------------
    
    
    union{
        tCAN msg;
        
        struct{
            unsigned int dummy1;
            unsigned char dummy2;
            unsigned char dummy3;
            unsigned char dummy4;
            unsigned int tstamp;
            unsigned int sData;
        };
    }in;
    
    
    
    
    
    
    int main() {
    char i;
        
        for(i=0;i<8;++i){
            in.msg.data[i]=i+1;
        }
        
        for(i=0;i<8;++i){
            printf("data%i: 0x%02x\n",i,in.msg.data[i]);
        }
        
        printf("\n");
        
        in.tstamp=0x12345678;
        in.sData=0xabcd;
        
        for(i=0;i<8;++i){
            printf("data%i: 0x%02x\n",i,in.msg.data[i]);
        }
    
    
    }
    what I am getting is this:

    data0: 0x01
    data1: 0x02
    data2: 0x03
    data3: 0x04 //this is as expected.
    data4: 0x05
    data5: 0x06
    data6: 0x07
    data7: 0x08


    data0: 0x01 //why is data0 0x01
    data1: 0x78
    data2: 0x56
    data3: 0x34
    data4: 0x12
    data5: 0xcd
    data6: 0xab
    data7: 0x00

    expected output (maybe I'm wrong here):
    data0: 0x78
    data1: 0x56
    data2: 0x34
    data3: 0x12
    data4: 0xcd
    data5: 0xab
    data6: 0x00
    data7: 0x00

    Anybody can suggest why I'm getting this output?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Alignment.
    All your chars in your tCan have zero padding.
    In your anonymous struct in the union, the int following 3 chars has a padding byte.
    Result, your data is skewed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    Quote Originally Posted by Salem View Post
    Alignment.
    All your chars in your tCan have zero padding.
    In your anonymous struct in the union, the int following 3 chars has a padding byte.
    Result, your data is skewed.
    OK. thank you for your explanation.

    How can I get round this?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Get around what?
    What exactly are you trying to do?

    If you want to put ints directly into data, then use memcpy.
    myint = 1234;
    memcpy(&in.data[0],&myint,sizeof(myint));

    Note there is no way to directly access data[0]..[3] as an integer because it is misaligned with respect to integer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    Quote Originally Posted by Salem View Post
    Get around what?
    If you want to put ints directly into data, then use memcpy.
    myint = 1234;
    memcpy(&in.data[0],&myint,sizeof(myint));

    Note there is no way to directly access data[0]..[3] as an integer because it is misaligned with respect to integer.
    I'll give that a go... thank you

  6. #6
    Registered User
    Join Date
    Feb 2018
    Posts
    4
    Quote Originally Posted by Salem View Post
    memcpy(&in.data[0],&myint,sizeof(myint));
    That works a charm for what I need to do! thanks!
    Last edited by sherzaad; 02-27-2018 at 05:50 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can anyone explain me why output is this ?
    By san12345 in forum C Programming
    Replies: 3
    Last Post: 02-26-2016, 01:45 AM
  2. why is the output 4,4,4? pls explain
    By surbhijain93 in forum C Programming
    Replies: 3
    Last Post: 10-04-2014, 02:52 AM
  3. Please explain why output is 4??
    By ackr1201 in forum C Programming
    Replies: 6
    Last Post: 07-17-2011, 03:20 AM
  4. explain the output ?????
    By shashwat in forum C Programming
    Replies: 4
    Last Post: 07-09-2011, 06:32 AM
  5. help: please explain the output
    By soniclavier in forum C Programming
    Replies: 8
    Last Post: 12-07-2009, 12:35 PM

Tags for this Thread