Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: bitwise operator on a struct (http://www.velocityreviews.com/forums/t313794-re-bitwise-operator-on-a-struct.html)

Peter Shaggy Haywood 06-24-2003 11:59 PM

Re: bitwise operator on a struct
 
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"?


All times are GMT. The time now is 09:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.