Path: news.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!moe.ksu.ksu.edu!bigboy.sbc.com!news.mtholyoke.edu!news.byu.edu!gatech!howland.reston.ans.net!usc!cs.utexas.edu!geraldo.cc.utexas.edu!geraldo.cc.utexas.edu!usenet From: brianh@ccwf.cc.utexas.edu (Brian Huddleston) Newsgroups: comp.sys.apple2.gno Subject: Pointer problem... Date: 20 Feb 1993 08:07:12 GMT Organization: The University of Texas at Austin, Austin TX Lines: 32 Message-ID: <1m4orgINNhtm@geraldo.cc.utexas.edu> NNTP-Posting-Host: sleepy.cc.utexas.edu I have a question for all of you here in comp.sys.apple2.gno.c.orca.tutorial: The following program works correctly on my unix box.... main() { int *ptr; static int zilch[] = {1, 2, 3, 4, 5, 0}; ptr=zilch; while (*ptr != 0) { printf("The value stored at addr %u is %d.\n",ptr,*ptr); ++ptr; } printf("The value of *ptr is %d!\n",*ptr); printf("The value of *(ptr-2) is %d!\n",*(ptr-2)); } But under ORCA/C the output is... The value stored at 50186 is 24. The value stored at 50188 is 24. The value stored at 50190 is 24. The value stored at 50192 is 24. The value stored at 50194 is 24. The value of *ptr is 0! The value of *(ptr-2) is 4! Does anyone know what is going on here? Thanks. brianh@ccwf.cc.utexas.edu Path: news.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!moe.ksu.ksu.edu!bigboy.sbc.com!news.mtholyoke.edu!news.byu.edu!gatech!howland.reston.ans.net!usc!cs.utexas.edu!geraldo.cc.utexas.edu!geraldo.cc.utexas.edu!usenet From: daveh@ccwf.cc.utexas.edu (Dave Huang) Newsgroups: comp.sys.apple2.gno Subject: Re: Pointer problem... Date: 20 Feb 1993 08:38:36 GMT Organization: The University of Texas at Austin, Austin, Texas Lines: 15 Message-ID: <1m4qmcINNibc@geraldo.cc.utexas.edu> References: <1m4orgINNhtm@geraldo.cc.utexas.edu> NNTP-Posting-Host: daisy.cc.utexas.edu Originator: daveh@daisy.cc.utexas.edu In article <1m4orgINNhtm@geraldo.cc.utexas.edu> brianh@ccwf.cc.utexas.edu (Brian Huddleston) writes: >I have a question for all of you here in comp.sys.apple2.gno.c.orca.tutorial: >The following program works correctly on my unix box.... *snip snip* > printf("The value stored at addr %u is %d.\n",ptr,*ptr); Try changing the %u to a %lu since pointers are 32-bit (long) values... -- Dave Huang | Internet: daveh@ccwf.cc.utexas.edu | "Microwaves: They're not just or { khym | pekb364 }@utxvms.cc.utexas.edu | for cooking anymore." America Online: DrWho29 | Path: news.weeg.uiowa.edu!news.uiowa.edu!hobbes.physics.uiowa.edu!moe.ksu.ksu.edu!bigboy.sbc.com!news.mtholyoke.edu!news.byu.edu!gatech!swrinde!cs.utexas.edu!geraldo.cc.utexas.edu!geraldo.cc.utexas.edu!usenet From: daveh@ccwf.cc.utexas.edu (Dave Huang) Newsgroups: comp.sys.apple2.gno Subject: Re: Pointer problem... Date: 20 Feb 1993 09:41:07 GMT Organization: The University of Texas at Austin, Austin, Texas Lines: 29 Message-ID: <1m4ubjINNj66@geraldo.cc.utexas.edu> References: <1m4orgINNhtm@geraldo.cc.utexas.edu> <1m4qmcINNibc@geraldo.cc.utexas.edu> <1m4s55INNin5@geraldo.cc.utexas.edu> NNTP-Posting-Host: sneezy.cc.utexas.edu In article <1m4s55INNin5@geraldo.cc.utexas.edu> brianh@ccwf.cc.utexas.edu (Brian Huddleston) writes: >In article <1m4qmcINNibc@geraldo.cc.utexas.edu> daveh@ccwf.cc.utexas.edu (Dave Huang) writes: >>Try changing the %u to a %lu since pointers are 32-bit (long) >>values... > >Well, at the moment I'm more worried about the %d than the memory location. >Why does it work outside of the while loop and not inside it. I should >get a nice 1,2,3,4,5 instead of a procession of 24's. Should I have >declared the *ptr as a longint? Nope, just use %lu and it'll work: The value stored at addr 983373 is 1. The value stored at addr 983375 is 2. The value stored at addr 983377 is 3. The value stored at addr 983379 is 4. The value stored at addr 983381 is 5. The value of *ptr is 0! The value of *(ptr-2) is 4! The %u tells printf that you're going to give it a 2-byte int, but since you give it a 4-byte value instead, two bytes are left over that get grabbed by the %d... that's why you see the 24s (24 is probably the the bank number). -- Dave Huang | Internet: daveh@ccwf.cc.utexas.edu | "Microwaves: They're not just or { khym | pekb364 }@utxvms.cc.utexas.edu | for cooking anymore." America Online: DrWho29 |