Sub-number Han number & #183; C Language -- pointer, C language pointer

Source: Internet
Author: User

Number of sub-classes · C Language -- pointer and C language pointer

1. What is a pointer?

Pointer explanation in Baidu: it is an object in programming language. With an address, its value directly points to the value in another place in computer memory.

That is to say, the pointer is used to point to a memory unit.

Simplified,Pointer is the address.

 

Ii. Declare pointer

1. Method: Data Type * Name Initialization

2. For example:

Int * p = & a // Where int Is the data type, p is the name of the pointer variable, and = & a is the initialization of the pointer Variable p

3. Note: the pointer is declared completely.Be sure to initializeOtherwise, it will become a wild pointer (not pointing to the target); and when the pointer is initializedOnly addresses can be pointed, Cannot point to data (as described in * and ).

 

3. Use Pointer

A pointer is also a variable, which is used in the same way as other variables.

The wild pointer and the NULL pointer are different.If you use the wild pointer, the program reports an error.

 

Iv. * And &

1. *: it has two functions: one is to declare a variable, which is used to identify the variable as a pointer variable; the other is to act on the pointer elsewhere, * represents the value, that is, * p takes the value pointed to by the pointer p.

(* When it is a declaration, it acts as a declaration Operator. when it acts on a pointer elsewhere, it acts as a single-object operator. There is also a situation where, * Indicates the binary operator, that is, the product meaning, for example, 5*5 = 25)

2. &: return the address operator. For example, & a is the address of variable.

 

5. Read the pointer in one case

# Include <stdio. h> int main (void) {int a = 10; // declare a variable a with a value of 10 int * p = & a; // declare a pointer Variable p, its value is the address printf of variable a ("the value of a is % d \ n", ); // output a printf ("the value of a is % d \ n", * p ); // output the value of p to printf ("the address of a is % d \ n", p ); // output the address printf of a ("the address of a is % d \ n", & a); // output the value of p return 0 ;}

 

Vi. NULL pointer

1. Purpose: After declaring a pointer, if it is not used for the moment, you can assign it NULL to avoid it being a wild pointer.

2. Method: pointer = (data type *) malloc (memory size you allocated)

3. For example:

Int * p; p = (int *) malloc (4); // allocate dynamic memory

4. Note: The malloc data type must be consistent with the pointer data type;

After the memory is allocated, it is customary to determine whether the allocation is successful. If the allocation fails, the program will exit;

The memory must be released after it is allocated., Free is the release function, so malloc and free must appear in pairs.

5. Case study:

# Include <stdio. h> # include <stdlib. h> // the malloc and free functions are stored in stdiib. h In this function library, int main (void) {int * p; p = (int *) malloc (4); // allocate dynamic memory if (p = NULL) {printf ("memory allocation failed. \ N "); exit (-1); // exit} * p = 100; printf (" content in p pointer = % d \ n ", * p ); free (p); // return 0 for memory release ;}

 

VII. Dual-level pointer

A two-level pointer is a pointer to a pointer, namely:

Int * p1 = & a; // pointer p1 points to the address of a int * p2 = p1; // pointer p2 points to p1

For example:

# Include <stdio. h> int main (void) {int a = 1; int * p1 = & a; int * p2 = p1; printf ("% d \ n", * p2 ); // output the value of the variable a pointed to by the pointer P2. return 0 ;}

 

8. Fan Wai

Note: in the previous case, if we change p = (int *) malloc (4) to 2, an error is returned. For exampleMemory out of bounds).

Why is this? This is about the data length, because at the time of VC6.0, the system and CPU grew to 32 bits, so VC6.0 is a 32-bit compiler. In VC6.0, the Data Length of int Is 32/8 = 4 bytes;

When TC2.0 was born, the system and CPU remained at 16 bits. Therefore, TC2.0 is a 16-bit compiler. In TC2.0, int Data Length is 16/8 = 2 bytes.

 

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.