Why do I need the "*" ?

Discussion in 'Mac Programming' started by CmdrLaForge, Mar 15, 2010.

  1. CmdrLaForge macrumors 68040

    CmdrLaForge

    Joined:
    Feb 26, 2003
    Location:
    around the world
    #1
    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
    
     
  2. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #2
    oops, too quick, hang on, lemme have another look

    EDIT: is it because you return the NSString by reference?
     
  3. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #3
    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 *.
     
  4. CmdrLaForge thread starter macrumors 68040

    CmdrLaForge

    Joined:
    Feb 26, 2003
    Location:
    around the world
    #4
    Hi

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

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #5
    This tutorial should help explain pointers better. Having a solid understanding of pointers is critical to C programming (objective-c too of course).
     
  6. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #6
    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.
     
  7. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #7
    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.
     
  8. ranguvar macrumors 6502

    Joined:
    Sep 18, 2009
    #8
    Way to go, grasshopper. Pointers aren't exactly a "little thing".
     
  9. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #9
    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
     

Share This Page