Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CmdrLaForge

macrumors 601
Original poster
Feb 26, 2003
4,658
3,194
around the world
Sometimes in programming the little things are the ones that throw me off. The big concepts I always get as they are usually explained very well. I have the following code and I just don't understand why I need the *colorname before the function name. Does it have to do with the function call within NSLog?

This is not the whole code, just an excerpts:

Code:
typedef enum {
	kRedColor,
	kGreenColor,
	kBlueColor
} ShapeColor;

NSString *colorName (ShapeColor color)
{
	switch (color) {
		case kRedColor:
			return (@"red");
			break;
		case kGreenColor:
			return @"green";
			break;
		case kBlueColor:
			return @"blue";
			break;
	}
	
	return @"no clue";
	
} // colorName

@implementation Rectangle

- (void) setFillColor: (ShapeColor) c
{
	fillColor = c;
} // setFillColor


- (void) setBounds: (ShapeRect) b
{
	bounds = b;
} // setBounds


- (void) draw
{
	NSLog (@"drawing a rectangle at (%d %d %d %d) in %@",
		   bounds.x, bounds.y, 
		   bounds.width, bounds.height,
		   colorName(fillColor));
} // draw

@end // Rectangle
 
All Objective-C classes have to be allocated on the heap, so you effectively have a pointer to your object, that is what the star is, a pointer. Nothing more, nothing less.

In your example function you are returning an NSString object which are all pointers to a char array wrapped up in CFoundation.

Usually all classes with NS are pointers, but not all of them, some are structs and aren't declared with a *.
 
All Objective-C classes have to be allocated on the heap, so you effectively have a pointer to your object, that is what the star is, a pointer. Nothing more, nothing less.

In your example function you are returning an NSString object which are all pointers to a char array wrapped up in CFoundation.

Usually all classes with NS are pointers, but not all of them, some are structs and aren't declared with a *.

Hi

thanks a lot. If the return type would be a int the function wouldn't be declared as a pointer?
 
To answer your question, yes if a function only returned an int,

it could have the function prototype

int funcA(int, int);

or it could have the function prototype

void funcA(int, int, int *);

Where the third parameter gets the result.

If the function is called

int* funcA(int, int);

All it means is you are returning a pointer to an int, you have no idea how many ints, you may have one, or you may have millions.

Once you understand pointers, C is one of the easiest languages in the world to program in.
 
I don't know about easiest -- Java and Python are both conceptually a little easier than C, though of course every language has its flaws.

But definitely take the time to learn about pointers and how they work. They can be a real headache, but they are very powerful.
 
Hi

thanks a lot. If the return type would be a int the function wouldn't be declared as a pointer?

Precisely, if the return type was "int", the function header would be:

Code:
int colorName (ShapeColor color)

Although this is just a 'cosmetic' point, I'd always write the original function this way; to me it makes more sense:

Code:
NSString* colorName (ShapeColor color)

That to me reads more clearly. "This is a function called colorName which returns a pointer to an NSString".

I think it's more consistent too with how you'd write the equivalent Objective-C method something like this :

Code:
- (NSString*) colorName: (ShapeColor) color
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.