Skip to contentSkip to conversation

code explaination

DISCUSSION vadiraj c s 3 participants 3 messages Sep 18, 2003 VFS

lore
Share & tools
lore
#0 lore raw

Please tell me the reason for the out put of the code

#include<unistd.h>
struct xyz{

       int i;
       int j;
      } pqr = {100,300} ;

int main()
{
struct xyz *z = &pqr;
abc_char(z);
abc_int(z) ;
}

void abc_char(char *i)
{
printf("char abc %d",*i) ;
}

void abc_char(int *i)
{
printf("int abc %d",*i) ;
}

out put:
char abc 0
int abc 300;
swap the structure value to i=300,j = 100;
you get
char abc 1;

can you type cast structure add to any datatype?
what can be the reason for such output;

regards
vadiraj
___________________________________________________
Catering meets Camera; Tanya Weds Sahil. Rediff Matchmaker strikes another interesting match Visit http://matchmaker.rediff.com?1

#1 lore raw

The output of abc_char depends on the platform you're running it on. When you type cast your structure to a char, you're effectively accessing the lowest byte of pqr.i. On x86, which is little endian, this will contain the value 100 (0x64). On a big endian machine this will be zero. When you access pqr.i = 300, the lowest byte will be 44 (0x2c) on little endian and zero on big endian.
Two more points:
1. Writing such code is a Bad Thing, since it relies on the endianness of the platform, and the way the compiler pads structures.
2. This discussion does not belong to linux-fsdevel, since it is a very basic C question and is in no way relevant to Linux F/S development.

Regards,
Akshat

Quoted 39 lines · vadiraj c s

Please tell me the reason for the out put of the code

#include<unistd.h>
struct xyz{

        int i;
        int j;
       } pqr = {100,300} ;
int main()
{
    struct xyz *z = &pqr;
    abc_char(z);
    abc_int(z) ;
}

void abc_char(char *i)

{
   printf("char abc %d",*i) ;
}

void abc_char(int *i)

{
   printf("int abc %d",*i) ;
}

out put:
char abc 0
int abc 300;
swap the structure value to i=300,j = 100;
you get
char abc 1;

can you type cast structure add to any datatype?
what can be the reason for such output;

regards
vadiraj

#2 lore raw
Quoted 3 lines

On x86, which is little endian, this will contain
the value 100 (0x64). On a big endian machine this will be zero. When you access pqr.i = 300, the lowest byte will be 44 (0x2c) on little endian

and

Quoted 1 line

zero on big endian.

That doesn't explain the behavior he asked about:

Quoted 6 lines
Quoted 6 lines

output:
char abc 0
int abc 300;
swap the structure value to i=300,j = 100;
you get
char abc 1;

(i.e. the values are 0 and 1, not 100 and 44 or 0 and 0).

Not only that, the "int abc 300" line is inconsistent with the program posted, and the program posted won't even compile because there's a typo in the name of the abc_int() definition.

It looks like there's a lot of reason to believe that there is just an experimental error here.

Quoted 22 lines

#include<unistd.h>
struct xyz{

        int i;
        int j;
       } pqr = {100,300} ;
int main()
{
    struct xyz *z = &pqr;
    abc_char(z);
    abc_int(z) ;
}

void abc_char(char *i)

{
   printf("char abc %d",*i) ;
}

void abc_char(int *i)

{
   printf("int abc %d",*i) ;
}