C pointers vs. Objective-C pointers

前端 未结 7 1546
醉酒成梦
醉酒成梦 2021-02-06 05:37

I\'m coming from an Objective-C background and am trying to expand my knowledge in C. One thing has me confused, however, and that\'s the difference between pointers in C and O

相关标签:
7条回答
  • 2021-02-06 05:46

    The question is - are your strings const or not?
    The first example would probably work.
    Even in non-objective C compilers, literal strings can be placed in const memory via linker / compiler options.

    On this line: stringPointer = &string; You are copying the address of the pointer into the string poitner. Clearly incompatible.

    And on this line:
    *stringPointer = @"Chocolate milkshake";

    You are trying to write a string into the pointer (the pointer is a 4 byte address). Not a good idea to copy a whole string on top of it.

    0 讨论(0)
  • 2021-02-06 05:50

    The thing is that when you create an object, you actually always manipulate it through a pointer assigned to it, hence (NSString *).

    Try doing the same thing in C (working with a string), perhaps it becomes clearer:

    void myFunction()
    {
        char *string = "this is a C string!";
        char **ptr=&string;
    
    
        // Prints: "this is a c string"
        printf("ptr points to %s: \n",  *ptr);
    }
    

    As you can see, pointers work in exactly the same way as they do in objective-c. Bare in mind that there are very few primitives in objective-c (int is the most obvious one). Most of the time you are creating a pointer of type object X (say NSString), and then allocating a chunk of memory (through [[Object alloc] init]) and assigining the start address of that chunk to your pointer. Exactly the same as we've done in C with our string.

    0 讨论(0)
  • 2021-02-06 05:55

    &string has type NSString **, whereas stringPointer has type NSString *, thus the warning. Then you try to assign an instance of NSString (with type NSString *) to a variable of type NSString, thus the error.

    0 讨论(0)
  • 2021-02-06 05:56

    Your NSString *string is itself a pointer. Therefore, in order to point to it, you need to declare stringPointer as a pointer to a pointer. That is, declare stringPointer like this:

    NSString **stringPointer;
    

    Then everything should work. Note that the same pointer semantics apply in C and Objective-C.

    0 讨论(0)
  • 2021-02-06 05:58

    In your first example, x and pointerX have different types ((int) and (int *) respectively). In your second example, string and stringPointer have the same type (NSString *). Try instead:

    NSString *string = @"Caramel coffee", **stringPointer;
    
    0 讨论(0)
  • 2021-02-06 06:04

    What are you trying to do?

    Not sure if it will work as you intended.

    You seem to be taking concepts from C and apply it to Cocoa classes, I thought you were learning C. Have you seen anywhere in Objective-C code taking address of an object?

    Cocoa classes are implemented using Class clusters which mean that they share the same interface but you will get specific extended class which you manipulate.

    In your case you are taking address of possibly class that extends NSString and assign it to pointer to NSString.

    Example:

    NSString * str = @"Caramel coffee";
    NSString * str2 = [NSString stringWithString:@"all"];
    
    NSLog(@"%@", [[str class] className]);
    NSLog(@"%@", [[str class] className]);
    

    Output (GNUStep linux):

    2009-12-08 10:49:29.149 x[25446] GSCInlineString
    2009-12-08 10:49:29.149 x[25446] NSConstantString
    

    ... apart from the obvious pointer definition problems pointed out by others.

    0 讨论(0)
提交回复
热议问题