[Learning notes] [oc] pointer, learning notes oc

Source: Internet
Author: User

[Learning notes] [oc] pointer, learning notes oc

 

1. Take the value of a variable, either directly or indirectly

Direct: Access variable

Indirect: obtains the value of the variable through the memory address of the variable.

The variable that stores the memory address is the pointer variable.

2. syntax for defining pointer variables: type * variable name

&: Obtains the address operator and the memory address of the variable. (single object operator)

*: Gets the variable operator and reads the variable referred to by the pointer.

3. There are two rules for pointer variables:

(1) When defining pointer variables, you must use * to identify and define pointer variables;

(2) the C language is a strong type language, and the pointer variable must be defined before use. Once the pointer type is specified, the pointer variable can only point to the specified type of variable.

4. pointers can be used as function parameters:

# Import <Foundation/Foundation. h>

Void swap (int * a, int * B)

{

Int tmp = *;

* A = * B;

* B = tmp

A = B = nil;

}

Int main (int argc, char * argv [])

{

@ Autoreleasepool {

Int m = 5;

Int n = 9;

// The m and n values are reversed.

// If the function parameters use common variables, m and n values are not affected by the function

Swap (m, n );

NSLog (@ "% d, % d", m, n );

}

Return 0;

}

 

Cause: because the function is passed by value, a copy of the variable is directly used. If the function variable is a common variable, the changes made by the function to the variable cannot change the variable itself.

If the function needs to modify the value of the variable, you need to pass in the pointer of the function and modify the variable value referred to by the pointer.

Take the function swap (a, B) as an example:

Pointer * a points to m, and the value of m (m = 5) is assigned to tmp, and tmp assigns the value to pointer * B, pointer * B Represents the memory address of n, thus changing the value of n (address transfer );

If the parameter is a common variable: because it is a value transfer, it uses a copy of m and n, so we simply copy m and n to a, B;, B Does not affect m or n.

 

5. pointers and Arrays

(1) The first address of the array is the address of the array.

Int arr [];

& Amp; arr [0] = & amp; arr [];

You cannot assign an integer directly to a pointer variable.

(2) pointer assignment

Int * p; int * pt; int;

P = & a; // assign the memory address of a to p;

P = & arr [I]; // assign an element address in the array to p;

P = arr; // assign the first address of the array to p;

P = pt; // assign the memory address stored in pt to p;

(3) array pointer operation

Add or subtract an integer n: add or subtract the pointer address (n * variable size byte );

When two pointers direct to the same array, the two pointers subtract from each other to indicate the number of elements separated by the two array elements;

When the two pointers point to the same array, the pointer to the first element is smaller than the pointer to the next element.

(4) array variables are pointer constants because the array address is immutable.

 

6. array variables as function parameters:

The input is a pointer to a value. changes made to the array elements in the function will affect the original array.

Quick sorting:

//

7. String and pointer

The C language uses character arrays to represent strings. Therefore, the string pointer is the array pointer pointing to the first address of the character array.

String (character array), character pointer (pointer to the character array)

Ps: You need to define the pointer As a struct pointer.

Character arrays cannot be re-assigned. They can only be assigned at definition;

8. Functions and pointers

The pointer can also represent the function entry.

The Pointer Points to the function entry step:

(1) define the function pointer variable:

Syntax: (function return value type *) (function pointer variable name )()

(2) assign a function to the pointer variable:

(3) call a function using a function pointer variable:

Syntax: (* function pointer variable name) (parameter );

Function pointer as another function parameter:

When defining a function, but some processing logic is not determined, that is, when some code needs to be dynamically changed, the function pointer can be used as a parameter.

Function pointer as return type:

To ensure that the returned pointer is valid, there are two methods:

(1) Use the static keyword when pointing to a local variable in the function;

(2) point the pointer to data that will not be released temporarily.

 

9. pointer Array

Declare the format of the pointer array: Data Type * array variable name [length]

Note: type (* array variable) [length]: indicates a pointer to an array.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.