Getting started with iOS development☞C language (Advanced pointer, judgment type), ios pointer

Source: Internet
Author: User

Getting started with iOS development☞C language (Advanced pointer, judgment type), ios pointer
Advanced pointer

Advanced pointers: including double pointers (second-level pointers), void pointers, and function pointers.

1. Double pointer

A double pointer (second-level pointer) is a pointer of a pointer.

2. void pointer

Void pointer: a universal pointer that represents any type of pointer.

When malloc allocates heap memory, the memory storage type cannot be determined, so void * can be used to represent any pointer type.

  

3. function pointer
  • A function pointer is a pointer variable pointing to a function. Each function has an entry address. The entry address of the function is saved in the pointer variable.
  • Each function has an entry address, which is the address pointed to by the function pointer. With the pointer variable pointing to the function, you can use the pointer variable to call the function, just as you can use the pointer variable to reference other types of variables.
  • Function pointers have two purposes: Calling functions and making function parameters.

 

Function pointer declaration method:

Return Value Type (* pointer variable name) ([parameter list]);

 

Definition Format of function pointer (pointer to function:

  

A function also occupies a storage space, because the function also occupies a storage space, so the function also has its own address.

The function address is saved in the function name. The function name is the function address = the array name is the array address.

The array name is the constant pointer pointing to the first element of the array, and the function name is also the constant pointer pointing to the first instruction of the function.

 

Syntax format of the function pointer:

  

  

Advantages and disadvantages of function pointers:

  • Advantage: if another function is used in a common function, but the function name is not fixed, it is compiled by the user. In this case, the function pointer must be used.

For example, for general point calculation, the computed function is to be determined by the caller.

For example, the general sorting function is also used, and the comparison function and the exchange function are also compiled by the user.

  • Disadvantage: The program crashes easily because the function pointer points to is not fixed.

  

 

Judgment type

How to determine the type: from the right to the left, without parentheses, you can see what type is on the far right. With parentheses, you can see what type is on the right.

 

  • Int * a [10] pointer Array
    • [] Has a higher priority than *. a represents an array, and its array element storage type is int *;
    • The value of a represents the address value of the first element a [0] of the array. It is a pointer, that is, * a = a [0]. Note that the pointer type is const, that is, it cannot be changed.
  • Int (* a) [10] array pointer
    • A Represents an array pointer pointing to an array of the int [5] type. Because of the type, after a ++, it will offset the sizeof (int) * 5 bytes.
  • (Int) a parameter is a function.

    •  

      Int (* a) (int) function pointer

    •  

      Int * a (int) pointer Function

The difference between a function and a method: a function is owned by everyone and has no class. Methods are class-based and not all users can use them.

 

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.