C pointers vs. Objective-C pointers

前端 未结 7 1552
醉酒成梦
醉酒成梦 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.

提交回复
热议问题