|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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 |
|
|||
|
"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 |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|