• 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
Is pointer syntax logical?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Is pointer syntax logical?


  • Subject: Is pointer syntax logical?
  • From: Bob Ueland <email@hidden>
  • Date: Thu, 26 Jul 2007 00:58:45 -0700 (PDT)

Why does the declaration of a pointer to an object look like this
  Fraction *aFraction;
instead of like this

  (Fraction *) aFraction;

-----------------
I’m reading Kochan’s book “Programing in Objective-C” and have come across pointers. What confuses me is the syntax associated with pointers. For instance Kochan defines a class called Fraction and uses it in the main program like this:
  Fraction *aFraction = [[Fraction alloc] init];
which I read as “create a Fraction object and let the pointer aFraction of type Fraction point to it”.

Now if I understand things correctly the above line is equivalent to the following two lines of code
  Fraction *aFraction;
  aFraction = [[Fraction alloc] init];
and not to
  Fraction *aFraction;
  *aFraction = [[Fraction alloc] init];

In the line
  Fraction *aFraction;
the Fraction is the type and aFraction is a variable. In my mind it seems that the asterisk belongs more to the type Fraction then to the variable aFraction.

Look at the following line of code:
  -(Fraction *) add: (Fraction *) f
Here we are declaring a method that takes a fraction object as input and returns a fraction object as output. Here we clearly see that the asterisk belongs more to the type Fraction then to the variable f.

So if I was to define the syntax of C I would rather define the declaration of a pointer like this:
  (Fraction *) aFraction;
But when K&R defined the syntax of C they used (what from my naive point of view doesn’t  seem logical)
  Fraction *aFraction;
So my question is why the syntax for pointers looks like it does. Is there any logical explanation.

Thanks Bob





____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Is pointer syntax logical?
      • From: Ian Joyner <email@hidden>
    • Re: Is pointer syntax logical?
      • From: Daniel DeCovnick <email@hidden>
    • Re: Is pointer syntax logical?
      • From: Alastair Houghton <email@hidden>
    • Re: Is pointer syntax logical?
      • From: "Juan Nunez-Iglesias" <email@hidden>
  • Prev by Date: Re: Saving images to MySQL
  • Next by Date: Re: Simple Bluetooth Question
  • Previous by thread: Combining predicates from multiple NSSearchFields, using IB bindings only
  • Next by thread: Re: Is pointer syntax logical?
  • Index(es):
    • Date
    • Thread