• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
C pointer manipulation (was Re: C question for you old guys ;-))
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

C pointer manipulation (was Re: C question for you old guys ;-))


  • Subject: C pointer manipulation (was Re: C question for you old guys ;-))
  • From: "Alastair J.Houghton" <email@hidden>
  • Date: Mon, 23 Jun 2003 00:05:27 +0100

On Sunday, June 22, 2003, at 11:24 pm, Martin Hdcker wrote:

Hi there,

to get back on topic of programming, could anybody of you tell me why this bit of code produces two different results?

freeSpacePointer adress = 0x00040000; // Really a ((unsigned char *) 0x00040000)
long tryToGet = 1024; // 0x400

// return freeSpacePointer + tryToGet; /* Doesn't work... Why? */
// returns 0x42000
// versus

return (freeSpacePointer)((long)adress + tryToGet);
// returns 0x40400 -- what I wanted to get

The reason is probably that freeSpacePointer is defined as a pointer to a type with a size of eight bytes (although as you haven't included the typedef, it's hard to tell); if you write

type *ptr;

then do

ptr + number

where number evaluates to an integer of some sort (like in your example), your C compiler will actually compute

ptr + number * sizeof type

This behaviour is useful in many cases, but it can be a pain if you just want to do some quick address manipulation, and it sometimes (perhaps even regularly?) surprises newbies.

Regards,

Alastair.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: C pointer manipulation (was Re: C question for you old guys ;-))
      • From: Martin Häcker <email@hidden>
References: 
 >Re: C question for you old guys ;-) (From: Martin Häcker <email@hidden>)

  • Prev by Date: Re: Use libcddb in my Cocoa App
  • Next by Date: Re: Use libcddb in my Cocoa App
  • Previous by thread: Re: C question for you old guys ;-)
  • Next by thread: Re: C pointer manipulation (was Re: C question for you old guys ;-))
  • Index(es):
    • Date
    • Thread