Re: bitwise operator on a struct

Discussion in 'C Programming' started by Peter Shaggy Haywood, Jun 25, 2003.

  1. Groovy hepcat asm_fool was jivin' on 21 Jun 2003 04:52:36 -0700 in
    comp.lang.c.
    Re: bitwise operator on a struct's a cool scene! Dig it!

    >I have made some corrections accordingly.
    >
    >#include <stdlib.h>
    >#include <stdio.h>
    >#include <string.h>
    >struct node
    >{
    > int y;
    >}l[]={3};
    >int main()
    >{
    > struct node *p=l;
    > int *k;
    > printf("%p %d\n",*p,p->y);


    This is still wrong. You are still passing a struct node to printf()
    but telling it to expect something else (a pointer to void this time).

    > k = (int*)p;
    > *k = (*k << 1);


    Tip: the above line can be shortened to this:

    *k <<= 1;

    > memcpy(p,k,sizeof *p);


    And this is still as pointless as it was before, since k points at
    the same place as p (because you assigned p's value to k above). And
    it still causes undefined behaviour, since the memory pointed at by k
    overlaps that pointed at by p. (In fact, they point at the same
    memory.)

    > printf("%p %d\n",*p,p->y);


    And this is still wrong. See above.

    > return 0;
    >}
    >
    >
    > Since it appears that your grasp of C is
    >> as yet not very firm, I'd say you would be making a SERIOUS
    >> MISTAKE to even consider such a thing; the chances of getting
    >> it right are vanishingly small at your present stage of
    >> development.

    >
    >How can a self-learner like me will improve his programming? Please
    >suggest something.


    Keep reading books on C. Keep writing C programs. (But start with
    something easy and work your way up.) Keep asking for help.

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
    Peter Shaggy Haywood, Jun 25, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. littlehobo

    Bitwise operator question...

    littlehobo, Sep 30, 2003, in forum: C++
    Replies:
    10
    Views:
    6,041
    Ron Natalie
    Oct 1, 2003
  2. Peter Shaggy Haywood

    Re: bitwise operator on a struct

    Peter Shaggy Haywood, Jun 25, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,576
    Peter Shaggy Haywood
    Jun 25, 2003
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,372
    Chris Fogelklou
    Apr 20, 2004
  4. Manish_Ganvir
    Replies:
    13
    Views:
    1,570
    Keith Thompson
    Feb 14, 2005
  5. Ioannis Vranos
    Replies:
    8
    Views:
    334
    James Kanze
    Nov 14, 2008
Loading...

Share This Page