C language-pointer basis

If you want to learn more about the knowledge points of pointers, you can watch another blog of mine.

Advanced pointer: https://blog.csdn.net/qq_46485161/article/details/115037692

The concept of addresses and pointers

In order to clarify what a pointer is, it is necessary to figure out how the data is stored in memory and how it is read.

Each byte in the memory area has a number, which is the "address". If a variable is defined in the program, the system will allocate a memory unit to this variable when the program is compiled.

1. The method of accessing variable values ​​according to variable addresses is called "direct access", for example:

printf(%d″,i);
scanf(%d″,&i);
k=i+j;

2. Another way of accessing variable values ​​is called "indirect access". That is, store the address of variable i in another variable.

In C language, the pointer variable is a special variable, which stores the address. Suppose we define a pointer variable i_pointer to store the address of an integer variable, and it is allocated two bytes with addresses (3010) and (3011). You can use the statement: i_pointer = & i;
store the address of i (2000) in i_pointer. At this time, the value of i_pointer is (2000), which is the starting address of the unit occupied by the variable i. To access the value of variable i, you can use an indirect method: first find the variable i_pointer that stores the "address of i", take the address of i from it (2000), and then take out the value of i at the bytes of 2000 and 2001

Pointers and pointer variables

Definition of pointers and pointer variables :
The address of a variable is called the "pointer" of the variable.
For example, the address 2000 is a pointer to the variable i.
If there is a variable dedicated to storing the address of another variable (that is, a pointer), it is called a "pointer variable".
The above i_pointer is a pointer variable.

The value of the pointer variable (that is, the value stored in the pointer variable) is the address (that is, the pointer).
Please distinguish the two concepts of "pointer" and "pointer variable".

The pointer to the variable and the pointer variable to the variable
1. Define a
pointer variable. The general form of defining a pointer variable is
base type * pointer variable name;
int *ptr;

Explanation
The type of pointer variable: It indicates the type of data stored in the memory space pointed to by the pointer.
The " " in the definition means that the defined variable is a pointer variable. The variable name is ptr, not ptr.

Note: Only the address (pointer) can be stored in the pointer variable, do not assign an integer (or any other non-address type data) to a pointer variable

Assign pointer

1. Assign a value to the pointer through the & operator, for example:
ptr_var = &var;
2. Assign a value to the pointer through another pointer variable that points to the same type of data item, for example:
ptr_var2 = ptr_var;
3. Assign a symbolic constant NULL to the pointer variable
For example: float *ptr_var3=NULL;
Description: NULL is a null pointer, which means that the value of the pointer variable is meaningless. The purpose is to avoid illegal references to pointer variables that have not been initialized. The definition of NULL is in "stdio.h".
4. Assign values ​​to variables through pointers.
For example:

	int *ptr_var = NULL;
	int var = 100;
	ptr_var = &var;
	*ptr_var = 10;	/*等价于var = 10;*/
	/*如果ptr_var指向var,则把10赋给var*/

Pointer operator

& Is a unary operator, it returns the memory address of the operand.
For example:

int var=12,*ptr;
ptr = &var;

* Is the inverse operator of &, it is also a unary operator, returning the value in the memory location pointed to by the pointer.
For example:

int var=12,*ptr=&var;
int temp = *ptr;	//等价于temp=var;

Explanation
of "&" and " " operators: ( The precedence levels of "&" and " " are the same, but they are combined from right to left)

If the statement pointer_1=&a; pointer_2=&a; has been executed;
(1) pointer_2=&* pointer_1?
First perform the operation of *pointer_1, which is the variable a, and then perform the & operation. &* pointer_1 is the same as & a, that is, the address of variable a, is to assign & a (address of a) to pointer_2

(2) & a? &a ?
Perform & a operation first, get the address of a, and then perform the
operation. That is, the variable pointed to by & a, which is variable a.
The functions of & a and pointer_1 are the same, they are equivalent to the variable a. That is, & a is equivalent to a.

(3) ( pointer_1)++ and pointer_1++
pointer_1++: ++ sum is the same priority level, and the combination direction is from right to left, so it is equivalent to
(pointer_1++). Since ++ is on the right side of pointer_1, it is "post-added", so the original value of pointer_1 is first operated to
obtain the value of a, and then the value of pointer_1 is changed so that pointer_1 no longer points to a.
(*Pointer_1)++: *pointer_1 is equivalent to a, so the entire expression is equivalent to a++

Pointer arithmetic operations

Two forms: pointer ± integer or pointer-pointer
Note: ① The result of addition and subtraction of pointer and integer value is a pointer, which means that the pointer points to the memory after the pointer is moved down or up by the number of storage units (integer value) address. The size of the storage unit is the memory size required by the data type of the pointer.
② The pointer and pointer subtraction operation requires that the two pointers to be subtracted are of the same type, and the result is an integer, which represents the number of data between the two pointers.
E.g:

//假定var存储在地址1000中,因为整数的长度是 4 个字,ptr_var节的值将是1004
int var, *ptr_var;
ptr_var = &var;
ptr_var ++;	

When the pointer is incremented, it will point to the memory location of the next element of its type, and vice versa
Insert picture description here

Pointer comparison

Premise: Both pointers point to the same type of variable.
Assume that ptr_a and ptr_b point to a and b respectively
Insert picture description here

Pointer is passed by address

The pointer can be used as a parameter
. The address of the actual parameter is passed to the formal parameter,
allowing the function to access the memory location. The
called function can modify the value of the main calling program's parameter.
Example:

#include <stdio.h>
void swap(int *pi1, int *pi2)
{
    
    
int temp;
temp = *pi1;
*pi1 = *pi2;
*pi2 = temp;
}

void main()
{
    
    
int iNum1, iNum2;
iNum1 = 10;
iNum2 = 20;
swap(&iNum1, &iNum2);
printf(“iNum1=%d,iNum2=%d” ,iNum1,iNum2);
}

Pointers and one-dimensional arrays

An array contains several elements, and each array element occupies a storage unit in memory, and they all have corresponding addresses. Since pointer variables can point to variables, they can also point to arrays and array elements.

The so-called array pointer (the name of the array) is the starting address of the index group, that is, the address of the first element. The pointer to the array is a constant pointer

The address of an array element can be expressed in two ways:
①Add an "&" sign in front of the array element, for example: &ary[2]
②Array name + subscript, for example: ary + 2

Example:

#include <stdio.h>

int main()
{
    
    
	static int ary[10] = {
    
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	int i;
	int *p=NULL;
  	for (i = 0; i < 10; i ++)
	{
    
    
		printf("\ni=%d,ary[i]=%d,*(ary+i)=%d  ",i,ary[i],*(ary + i));
		printf("  &ary[i]= %X,ary+i=%X",&ary[i],ary+i);
	}
	for (p = ary; p < ary+10 ; p++)
	{
    
    	/*能否使用ary++?*/
		printf("\naddress:%x,value:%d",p,*p);
	}
	return 0;
}

Array as parameter passing

When one-dimensional array elements are passed as parameters, pass by value

When the entire array (array name) is passed as a parameter, it is passed by address. There are three equivalent ways of writing:

void TestArray(char ary[ ]);
void TestArray(char ary[LEN]);
void TestArray(char *ary);

Integer array as return value

To sum up, if there is a real parameter group and you want to change the value of the elements in this array in the function, the corresponding relationship of the actual parameter has the following 4 situations:
① Both the formal parameter and the actual parameter use the array name, such as:

void f(int x[],int n)
{
    
     ····
}
void main()
{
    
    
	int a[10];
	·····
	f(a,10)
}

② Use array names for actual parameters and pointer variables for formal parameters. Such as:

void f(int *x,int n)
{
    
     ····
}
void main()
{
    
    
	int a[10];
	·····
	f(a,10)
}

③The actual parameters and formal parameters are all pointer variables. E.g:

void f(int *x,int n)
{
    
     ····
}
void main()
{
    
    
	int a[10]*p=a;
	·····
	f(a,10)
}

④ The actual parameter is a pointer variable, and the formal parameter is an array name. Such as:

void f(int x[],int n)
{
    
     ····
}
void main()
{
    
    
	int a[10]*p=a;
	·····
	f(a,10)
}

Character pointer to a string constant

You can declare that the character pointer directly points to the string constant, for example, char* pStr="Welcome"; the character pointer pStr will point to the character constant "Welcome". "Welcome" is a string constant, so its value cannot be modified.

You can use character pointers to store and access strings.
Declare character pointer syntax: char* pStr;
declare string: char str[10]="hello";
use character pointers to point to strings: pStr=str;
you can use character pointers to access characters String, such as pStr[0]='a'; This code changes the first character to'a'

void main()
{
    
    
	char* pStr;
	char str[10]="hello";
	pStr=str;
	pStr[0]='a';
	printf("pStr = %s\n",pStr);
}  ==>pStr = aello

The difference between character array and character pointer

Insert picture description here

Character array as return value

#include <stdio.h>
char *print()
{
    
    
	char acStr[]="hello world";
	return acStr;>返回整个数组(数组首地址)
}
void main()
{
    
    
	char *pcStr;
    pcStr = print();
    puts(pcStr);
	return;
}

Result:
Insert picture description here
Cannot print hello world Reason:
The array in the function is a local variable, and the system automatically clears it when the function ends. If an address is returned at this time, the data allocated by the system next time may be accessed, and a
huge error may be caused if the location is modified.

Here you can putchar acStr[]="hello world"; 改为char *acStr="hello world";

Guess you like

Origin blog.csdn.net/qq_46485161/article/details/115035600