Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #16 (permalink)  
Old 06-09-2012, 06:40 AM
Ralph Spitzner
Guest
 
Posts: n/a
Default Re: Code problem

Keith Thompson wrote:
> Angel<angel+news@spamcop.net> writes:
> [...]
>> Anyway, the bottom line to the original poster is "don't mix up integers

[....]

Only scenario I could think of is on a 68k System where
one woud want to stick a function pointer into Address
4, generate a Trap and hope memory wasn't corrupted :-P


--
See why I hate Windows users?
All pain, no gain.
-Howard Chu

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #17 (permalink)  
Old 06-09-2012, 01:51 PM
TheGunslinger
Guest
 
Posts: n/a
Default Re: Code problem

On Fri, 8 Jun 2012 07:52:18 -0700 (PDT), rage <hansum.rahul@gmail.com>
wrote:

>Can someone please help me out on this?
>
>#include<stdio.h>
>main()
>{
> int i;
> int *j=10;
> i=j+19;


j is an address...


*j is a value



> printf("%d %d\n",j,i);


i = *j + 10 = 29

i = j + 10 = address + 10 == garbage

>}
>
>The output is a garbage value (86) on my pc and ideone. Should it not be 29?



IMHO

MJR
Reply With Quote
  #18 (permalink)  
Old 06-11-2012, 11:18 AM
Charles Richmond
Guest
 
Posts: n/a
Default Re: Code problem

"rage" <hansum.rahul@gmail.com> wrote in message
news:7081cbd2-1ff6-4591-89c0-e7680b330790@googlegroups.com...
> Can someone please help me out on this?
>
> #include<stdio.h>
> main()
> {
> int i;
> int *j=10;
> i=j+19;
> printf("%d %d\n",j,i);
> }
>
> The output is a garbage value (86) on my pc and ideone. Should it not be
> 29?


As others have correctly pointed out, you have violated a dozen or so
constraints.

Using "old style" C (prior to the ISO standards), here is the answer I would
give:

If you add an integer to a pointer, the integer is *scaled* by the size of
the object that the pointer points to. An "int" on your machine must be
four bytes, therefore 19 is multiplied by 4 before adding it to the 10.
Thus you get 86. (4*19+10)

One of the important things to remember here: arithmetic involving pointers
is *scaled*.

--

numerist at aquaporin4 dot com

Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:57 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.