Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: bitwise operator on a struct

Reply
Thread Tools

Re: bitwise operator on a struct

 
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      06-24-2003
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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Equivalence in use of bitwise | operator and + operator Ioannis Vranos C++ 8 11-14-2008 11:03 PM
How to calculate size of an int without using the sizeof operator but using bitwise operator Manish_Ganvir C Programming 13 02-14-2005 07:24 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
Re: bitwise operator on a struct Peter Shaggy Haywood C Programming 0 06-24-2003 11:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57