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

Re: Is pointer syntax logical?


  • Subject: Re: Is pointer syntax logical?
  • From: Ian Joyner <email@hidden>
  • Date: Fri, 27 Jul 2007 09:40:24 +1000

Off topic issues like this keep coming up because this is an example where C-based languages attempt to merge low-level programming with (pseudo) high-level syntax.

Pointers are simply an example where implementation which should be hidden and handled by the compiler is badly made explicit. Simply, this should just be:

Fraction aFraction;

The compiler handles all the referencing/dereferencing code for you. In C pointers you have to make sure your code has all the referencing in it with &s, etc. If you just set up the policy in the declaration, the compiler generates the code for you automatically, no need for refactoring (compilers are great refactoring tools). That's what high- level languages are about (and in this case, something Java does right).

There is a rare case where you might want the object put in line on the stack, or in another object, but this is better handled by explicit syntax (and if you change your storage policy, just change the declaration, recompile and the compiler handles all the refactoring).

Ian

On 26/07/2007, at 5:58 PM, Bob Ueland wrote:

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.



_______________________________________________

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: Jacob Engstrand <email@hidden>
References: 
 >Is pointer syntax logical? (From: Bob Ueland <email@hidden>)

  • Prev by Date: Re: Is pointer syntax logical?
  • Next by Date: Re: Quickest Drawing Method
  • Previous by thread: Re: Is pointer syntax logical?
  • Next by thread: Re: Is pointer syntax logical?
  • Index(es):
    • Date
    • Thread