• 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: Daniel DeCovnick <email@hidden>
  • Date: Thu, 26 Jul 2007 15:06:37 -0700

You bring up a good point, but it's an issue of using an operator (and worse, the same operator as dereference) as a declaration modifier instead of a reserved word. Not really sure why they did it that way, but think about it like this:

int *a;
would have been, in any language where dereference was not the same as pointer declaration:
int pointer a;
or perhaps:
pointer int a;
or maybe:
pointer<int> a;


Personally, I like the last one best, but all of them are an improvement on the first. The last one is, IMO, best because it most clearly conveys the intent of the programmer: he wants a machine-word wide integer containing an address in memory, which is treated in a particular fashion when operations use it; you can't treat the bits that represent a float and an int the same when doing arithmetic on them or printing them. Nor, for that matter, can you do the same to an int and a char. Consider:
int a = 90;
unsigned char b = 200;
If we add...
b += a;
it does not result in the same lhs value as:
a += b;
Having one declare a type, both automatic and heap-allocated, and not allowing dereferencing of a void *, is as close as C comes to strong typing. And it does not come any closer:


float f = 5.123;
float *fp = &f;
int *ip = (int *)fp;

is perfectly valid code. But you'll find that *ip is not 5. Rather, it's 1084485534. This may well be machine-dependent.

The second one ( int pointer a; )is the most consistent with the current pointer declaration syntax; for multiple declarations:

int a, b, pointer c, pointer d;

And it should be noted that in none of the cases does the dereference operator have to change:

int pointer a, b = 4;
a = &b;
*a = 5;

^Would be valid.

As one minor nitpick (and to keep this relevant to Cocoa), -(Fraction *)add:(Fraction *)aFraction takes a pointer to a Fraction object, not the Fraction object itself.

As for exactly why K&R decided to use *, already in use for multiplication and dereference, to create a pointer? Your guess is as good as mine.

-Dan

On Jul 26, 2007, at 12:58 AM, 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.


Thanks Bob


_______________________________________________

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

  • Prev by Date: Re: NSTimers and sleeping mac's
  • Next by Date: Re: Is pointer syntax logical?
  • Previous by thread: Re: [OT] Re: Is pointer syntax logical?
  • Next by thread: Re: Is pointer syntax logical?
  • Index(es):
    • Date
    • Thread