Learning pointers is very important for learning C language

In the process of C language learning, pointers are a very important part. Learning pointers is very important for C language learning.

Pointers and memory

If memory is equivalent to a street, the address is equivalent to the room number of the house in the street.

One byte (8 bits) contains an address. Each location in memory contains a unique address identifier, and each location contains a value.

Note: 1 The contents of the pointer variable (ie address), pointer to the contents of the variable, the address of the pointer.

2*p has different meaning when used as left and right values, respectively

*p is placed on the left side of "=" to indicate the space pointed to by p

*p on the right of "=" indicates dereferencing p

3 There are two illegal situations when initializing pointers

Such as: int * pa; → This is an uninitialized pointer, this approach is not acceptable

*pa=10;

Int *pret=fun();→ This is an illegal pointer and cannot be assigned directly to a pointer variable

*pret=20;

2. Pointer constants

example:

*((int *)0x0018ff44)=20

0x0018ff44 is a constant and cannot be directly referenced. It must be cast to an integer address.

Changed to: int a=10;

*((int *)0x0018ff44)=30;

At this point a=30;

Pointer pointer

Example: 1int *p=NULL;

Int **ptr=&p;→The second-level pointer variable stores the address of the first-level variable

2int a=10;

Int *p=&;→To change the content of a to 20, you can write *p=20 or **q=20

4. Pointer operation

1 For a pointer variable *p, the incremented byte after p+n depends on the data type of p, if it is a char type, then add n bytes, if it is an int type, increase n*4 byte.

2 The elements between pointer 1 and pointer 2 are equal to the number of elements between pointer 1 and pointer 2

3 Allows a pointer to an array element to be compared to a pointer to the last element of the array, but it is not allowed to compare with the memory pointing to the first element.

Example: Using a Pointer to Implement a Bubble Sorting Function

Void bubble_sort(int arr[], int sz)

{

Int i = 0;

Int j = 0;

Int tmp = 0;

For (i = 0; i < sz; i++)

{

For (j = 0; j <= sz - i - 1; j++)

{

If ((*arr + j)>(*arr + j + 1))

Tmp = *(arr + j);

*(arr + j ) = *(arr + j+1);

*(arr + j + 1) = tmp;

}

}

Sliding Patch Panel

Sliding Patch Panel,Sliding Patch Panel,Sliding Patch Panel Bracket,Sliding Patch Panel Box

Huizhou Fibercan Industrial Co.Ltd , https://www.fibercannetworks.com

Posted on