C Language 12th round: Hands

Source: Internet
Author: User

C Language 12th round: Hands

"Learning goals"

1. Pointers

2. Pointers and Arrays

A: the concept of pointers

Memory storage units are sorted by byte. Each byte is numbered. We call it an address. Because it is possible to find the desired memory unit through the address, we make the address a pointer. The pointer is a special variable, and the value stored in it is interpreted as an address in memory

Role:

(1) Pointers can effectively represent complex data structures, such as queues, stacks, linked lists, etc.

(2) Pointers can handle memory addresses like assemblies, providing support for dynamic memory allocations

(3) The pointer can realize the convenient use of the array and the string, improve the efficiency of the program

B: definition of pointer variable

Data type * pointer variable;

Such as:

   INT*P2;        /*P2 is a pointer variable that points to int type */       float*p3;      /*P3 is a pointer variable that points to float type */      CHAR*P4;       /*P4 is a pointer variable that points to char type */

Description : The data type does not refer to the type of the variable itself, but to the type of the target variable that the variable is pointing to. pointer variables can only point to variables of the same type .

Pointer definition # include <stdio.h> int main (void) {                   //Wild pointer: Not a null pointer, is a pointer to the released or visited garbage memory         int   *ptr1; PTR1 is the int pointer, the wild pointer, inside is the garbage address         char  *ptr2;//PTR2 is a char pointer, the wild pointer, inside the garbage address          //compiled in VC, will prompt Ptr1 and PTR2 did not initialize                   float *ptr3= NULL;  PTR3 is a float-type pointer, not a wild pointer, has been initialized to null                 //view address         printf ("(ptr1) =%p\n", ptr1);         printf ("(ptr2) =%p\n", ptr2);         printf ("(PTR3) =%p\n", PTR3);           return 0; }

C: Assignment of Pointers (Note : type to match )

(a) using the address operator &:

Such as:

int a =133;int * p1;p1= & A;    Use the address operator to assign a value to the pointer p1


(b) Assigning a pointer variable that already has pointers to a pointer variable

If (connect to above):

int* P2;

P2=P1; P1 and P2 point to variable a at the same time

(c) Assignment of pointers and arrays

   int a[5],*pa;   Pa=a;    (The array name represents the first address of the array, so you can assign a pointer variable PA to the array)  //can also be written as:   pa=&a[0];   The address of the first element of the array is also the first address of the entire array,               or, of course, the method of initializing the assignment:  int a[5];  int *pa=a;


(d) Assignment of strings and pointers.

Like what:

    char *pc;    pc= "C Language";       or assignment with initialization:    char *pc= "C Language";


PS: Do not agree to assign a number directly to the pointer variable.

Such as:

   int *p;   p=1000;    Types do not match. Hint error   //Improved mode   p= (int *) 100;//forced type conversion.

D: & and the  *  the Use

(a) legal use of the address character to get the address of a variable

Such as:

  int A;  int score[5]= {n  , a, 98, and A. &a,&score[0];    The operation is lawful  & (a+5);         This operation is illegal, cross-border  &a=123;       The operation is illegal, necessary mandatory conversions  &score;        The operation is illegal, score itself represents score's first address

(b) Use the * operator to indirectly access the value of the target variable to which the pointer is pointing ( type to match )

ITN  a = 234;int * p1;p1=& A;    P1 points to the array aprintf ("%d\n", *p);  The symbol * is to remove the contents of the address.

E: The pointer is added to the integer minus

Meaning: Represents the movement of a pointer

such as: P+n p-n p++ p--++p--p

Ps:

(1) The n is an integer. The move of an address cannot be a decimal.

(2) The addition indicates that the pointer p moves in the direction of the address increase.

(3) Subtraction means that the pointer p moves in the direction of the address decrease

(4) As for the length of the move, it is determined by the computer.

For example, set p to be a pointer to type (type). n is an integer expression, then p+ (or-) n is a new address. The value is p+ (or-) n*sizeof (type).

F: Pointers and Arrays

Because the storage units for each element in the array are continuously allocated. It is therefore possible to use pointers to access arrays, where the array name is the first address of the number.

such as: int a[]= "ABCDEFG";

A is the first address of the array. Equivalent to &a[0]

PS: Through the address, can be very quick, convenient access to the array of other elements. Methods such as the following:

First address + offset

a[I] can be converted to: * (A+i), * (&a[0]+i)

Comparison of pointers to arrays

Pointer

Array

The address of the saved data, regardless of what data is stored in the pointer variable, is treated as an address.

Save the data. The array name is the first address of the first element of the group.

Indirect access to data. Extract the data from this address by getting the contents (address) of the pointer variable.

Pointers can be used to access data in the form of pointers, and they can be used to access data using subscript form.

Direct access to data. The ability to access data in the form of pointers, as well as the ability to use the subscript to interview data.

Often used in dynamic data structures

Pass through often used to store a fixed number of

allocating memory using the Malloc,calloc,recalloc and free functions

Cannot delete allocated memory, can delete data

The mistake of "smiling at your fingertips" is inevitable. Hope to get everyone's correction

Reproduced when the original link (http://it.bangedushuren.cn/) Original, reproduced please specify

Copyright NOTICE: This article has [www.bangedushuren.cn] original, reproduced please specify http://dh.51zhui.cn/, thank you

C Language 12th round: Hands

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.