Skip to contentSkip to conversation

code explaination

DISCUSSION vadiraj c s 2 participants 2 messages Sep 19, 2003 VFS

lore
Share & tools
lore
#0 lore raw
Quoted 17 lines · On Thu, 18 Sep 2003 Bryan Henderson wrote :
Quoted 2 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

Quoted 1 line

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;

Quoted 18 lines

(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 9 lines

#include<unistd.h>
struct xyz{

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

void abc_char(char *i)

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

void abc_int(int *i)

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

-
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

I apologize for the syntax mistake, as you said it does not explain the behavior of the code.... I knew the output to be as he explained, but it din't whats the reason...

it was executed on x86 machine on linux platform, where size of int is 4 bytes...

//vadiraj
___________________________________________________
Television meets Accounts; Jyoti Weds Rajaram. Rediff Matchmaker strikes another interesting match !! Visit http://matchmaker.rediff.com?1

#1 lore raw

On 19 Sep 2003 05:21:39 -0000 "vadiraj c s" <[email protected]> wrote:

| On Thu, 18 Sep 2003 Bryan Henderson wrote :
| > >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.
| >
| >That doesn't explain the behavior he asked about:
| >
| > >> 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.
| >
| > > #include<unistd.h>
| > > struct xyz{
| > >
| > >         int i;
| > >         int j;
| > >        } pqr = {100,300} ;
| > > int main()
| > > {
| > >     struct xyz *z ;
|          z = &pqr;
| > >     abc_char(z);
| > >     abc_int(z) ;
| > > }
| > >
| > > void abc_char(char *i)
| > > {
| > >    printf("char abc %d",*i) ;
| > > }
| > >
| > > void abc_int(int *i)
| > > {
| > >    printf("int abc %d",*i) ;
| > > }
| 
| I apologize for the syntax mistake, as you said it does not 
| explain the behavior of the code.... I knew the output to be as he 
| explained, but it din't whats the reason...
| 
| it was executed on x86 machine on linux platform, where size of 
| int is 4 bytes...
| 
| //vadiraj

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is this cleared up now? (other than being the wrong m-l?)

If i is 100, I get (as expected on little-endian x86):
char abc 100
int abc 100

If i is 300, I get (as expected on little-endian x86):
char abc 44
int abc 300

--
~Randy