Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help Understanding Syntax

C6SilverC6Silver Posts: 51Registered Users
I am confused by the syntax below for the parts in bold:

-(void)someAction: (NSString *)str;

I am confused as to why the "*" is in the paranthesis and the "str" variable is not. I understand pointers but again it is the layout that I am not getting in this case.

Thanks for any help.
Post edited by C6Silver on

Replies

  • The_RThe_R Posts: 33Registered Users @
    edited April 2012
    C6Silver wrote: »
    I am confused by the syntax below for the parts in bold:

    -(void)someAction: (NSString *)str;

    I am confused as to why the "*" is in the paranthesis and the "str" variable is not. I understand pointers but again it is the layout that I am not getting in this case.

    Thanks for any help.

    When you write something like: int number; it means that number is of type int

    In the same way, when you have something like: int *number; Its better look at it like number is of type "int *" which of course means integer pointer. Even though the star is right before the variable name it actually is a "part of the type" if I can put it that way.

    Hence for the above reason you see it as: (NSString *)str where the type of the parameter is in the parenthesis and the name is outside.
  • kitkat007kitkat007 Posts: 14Registered Users
    edited May 2012
    The_R wrote: »
    When you write something like: int number; it means that number is of type int

    In the same way, when you have something like: int *number; Its better look at it like number is of type "int *" which of course means integer pointer. Even though the star is right before the variable name it actually is a "part of the type" if I can put it that way.

    Hence for the above reason you see it as: (NSString *)str where the type of the parameter is in the parenthesis and the name is outside.

    So it is the same right ?

    int number
    int *number (a integer pointer)
    (int *)number ( the same as the above) (i dont know if this is possible)
  • The_RThe_R Posts: 33Registered Users @
    edited May 2012
    kitkat007 wrote: »
    So it is the same right ?

    int number
    int *number (a integer pointer)
    (int *)number ( the same as the above) (i dont know if this is possible)

    If by same you mean that it conveys the same information related to the type of "number" then yes...But you'd use

    int *number

    and

    (int *)number

    in two very different situations.

    When you write int *number you are basically defining a variable that is an integer pointer.
    void doSomething()
    {
        int *number = NULL;
        ...
    }
    

    You write (int *)number when you are declaring/defining an Objective C method to specify that your method takes an integer pointer as a parameter
    -(void) doSomethingUsing:(int *)number
    {
        ...
    }
    

    The other situation where you'd see something like (int *)number is when you are doing a C style type cast..which is basically changing the type of a variable (if you don't already know that)
    int *number = (int *)pointer;
    
  • dickthedevdickthedev Posts: 163Registered Users @ @
    It has to do with the placement of the "*" symbol, as you know the"*" means a pointer to something.
    What is confusing is a lot of times we type our declaration like this this: NSString *myString; so makes it look like the"*" is part of myString rather then NSString while it is clearer if you type NSString* myString. this way makes NSString* looks as a whole.
    When I first started Obj-C, I was a bit puzzled by this as well, now I learn to look at it this way, since all OBJ-C objects all declared as pointers ` (except for id ), I simply replace the float in C style declaration float myFloat with NSString* myString;
    I hope this helps clear up some of the Obj-C maze. :)
Sign In or Register to comment.