week1--Learning functions, pointers, and strings

Some basic knowledge of functions

1. Format of function definition:
Return value type function name (parameter list)
{

Code Body
return x;(x is of type return value)
}

For example:

int add(int a,int b) (void Do not use this type return)
{
int sum=a+b;
return sum;
}

Function classification: library function; Functions that come with the system
Define functions: user-defined functions

2. Calls to functions:
The direction in which parameters are passed during a function call is to pass arguments to parameters
Variable names in functions in different functions can be renamed because variables have different scopes
(Extension: local variable: scope is a function, each local variable name can have the same name
Global variables; Scope is the whole program, variable names cannot be repeated
Local variable name and global variable name cannot be renamed, scope will conflict)

Formal parameters: The parameters in the function definition, which have no specific value, are also called formal parameters.
Actual parameters; A parameter passed by a function call procedure with a specific value, also known as an argument

When a function is called, the compilation system allocates a memory unit to the parameter.
After the call, the return value is returned to the memory, the other function information is destroyed, and the parameter memory unit is released.

An example is shown in the figure.
eg:

Parameterless function: empty parameter list

void wucan() //Parameter list has no parameters, function parameter type is empty
{}

(parameterless no return function: void wucanfan(void)
{} )
Parameter function: parameter list is not empty
eg:

int add(int a,int b) //(Do not return if void type)
{
int sum=a+b;
return sum;
}

3. Function use process:
Function declaration (not required if the function definition is placed before the function call)
eg:

(extern)int add(int a,int b);	//extern can be used without

Function Definition
function call
(Extension: Declaring variables does not take up storage space
Defining variables takes up storage space
A Declaration contains a definition, which is a special case of a declaration)

Extension: exit() function: can be used to terminate program execution when an exception is thrown

4. Multi-file programming:

Also called modular programming. Encapsulate functions of different functions into different files. One. c file and one. The h file is called a module.

Header file:.h file
					(Store global variables and function declarations inside)
Source file:.c file
						(call.h Files)
In general, we need to avoid errors caused by multiple inclusions of the same function in the header file. There are two methods:
	①.
#ifndef _HEAD_H_
#define _HEAD_H_

	Declare Statement
	
#endif

②.

#pragma once

	Declare Statement

The first is used more often. The first can be used in linux, while the second can only be used in VS or QT... in windows.

The specific process is as follows:

Some pointer sausage

1.Summary
 Memory: Memory, in computer components, used to store programs and data, auxiliary CPU Important parts of processing operations

Memory: internal memory, temporary program/Data, data power loss        For example: SRAM,DDR etc.
External memory: external memory, long-term data storage/Program, data power down without loss        For example: ROM,FLASH,Hard disks, discs, etc.
(Contrast in Single-chip Computer   RAM(Data Storage: Store variables, intermediate results of operations, flag bits, etc., lost power-off data,
			   ROM(Read-only memory: stored program, raw data or tables, power-off data is not lost)

Pointer Definition: In memory, an address points to a variable unit, and an address can be visually compared to a pointer. The pointer enables you to find the memory unit to which it is addressed. In general, the pointer is the memory address, and the memory address is the pointer

	*: Value Operator, Dimension Reduction
	&: Take Address Operator, Ascend Dimension
int a=10;	//Assign a directly
int* p=&a;	//Assign the address value of a to the integer pointer variable P()
*p=100	//Change the content of the address corresponding to p, indirectly change the value of a, where the output value of a is 100	
int p=&a;	//Define p as an integer variable
*(int*)p=100;	//(int*) is a cast that converts an integer variable P to an integer pointer variable

The type of the definition pointer must correspond to the variable type, otherwise the printed data will be wrong

For example:

void main()
{
	char ch='a';	//Equivalent to 97
	int* p=&a;
	//*p=1000; 	// Directly modifying the value of a pointer variable corresponding to a memory unit will cause an error
	printf("%d\n",ch);	//Print result is 97
	printf("%d\n",*p);	//The result is -858993567
}

All pointer types store memory addresses, which are unsigned hexadecimal integers
Special note: All pointer types are 4 bytes in size on a 32-bit operating system
All pointer types are 8 bytes in size on 64-bit operating systems
2. Special pointer:
(1) Field pointer: pointer variable (address) points to an unknown space (the nature of the address is unknown), that is, directly give the pointer variable any value (address), the address is unknown.
Field pointers are allowed in the program, but it is not recommended to assign a variable's value or a fixed value directly to the pointer. Since the operating system takes up 0~255 as a system and does not allow access operations, an error may occur in accessing the memory space corresponding to the field pointer.
For example:

void main()
{
	int* p=100;		//The pointer variable is assigned directly, so p is the wild pointer
	printf("%d\n",*p);
}

(2) Null pointer: Null pointer refers to the address of the memory address number 0 (often used for judgment)
So accessing the memory space corresponding to the null pointer is bound to fail
For example:

void main()
{
	int* p=NULL;	//The pointer variable is null or 0, so p is a null pointer
	printf("%d\n",*p);
}

(3) Universal Pointer: The memory address that can accept any type of variable is passed through
When a universal pointer modifies a variable's value, it finds the pointer type corresponding to the variable
For example:

void main()
{
	int a=10;
	void* p=&a;
	printf("The byte size of the Universal Pointer in memory:%d\n",sizeof(void*));	//The result is 4
	*(int*)p=100;
	printf("%d\n",*(int*)p);	//The result is 100, and if there is no cast (int*), an error will be reported
}

(4) Guidelines modified by const
const-modified variables are not allowed to be changed directly, but can be changed indirectly through the pointer
For example, 1:const modifier variable

void main()
{
	const int a=10;
	//a=100; 	// Writing this sentence to modify the value of a directly will result in an error
	int* p=&a;
	*p=100;
	printf("%d\n",*p);	//Indirectly changing the value of a through a pointer variable gives an output of 100
}

For example, 2:const modifies the pointer type (const int* p=&a)
You can modify the value of the pointer variable, not the value of the memory unit corresponding to the pointer variable

void main()
{
	int a=10;
	int b=20;
	const int*p=&a;
	p=&b;	//Executable	
	//*p=100; 	// Errors will occur
	printf("%d",*p);
}

For example, 3:const modifies pointer variables (int* const p=&a)
You cannot modify the value of a pointer variable, you can modify the value of the memory unit corresponding to the pointer variable

void main()
{
	int a=10;
	int b=20;
	int* const p=&a;
	//P=&b; 	// Errors will occur
	*p=100;	//Executable
	printf("%d",*p);
}

As you can see from 2 and 3, for a pointer, const modifies the nearest variable or type to it

For example, 4:const modifies first-level pointer variables and pointer types (const int* const p=&a)
You cannot modify the value of a first-level pointer variable or the value of a memory unit corresponding to a first-level pointer variable.

void main()
{
	int a=0;
	int b=0;
	const int* const p=&a;
	p=&a;	//Errors will occur
	*p=100;	//Errors will occur
}

However, for multilevel pointers, even if const modifies first-level pointer variables and pointer types, it can indirectly modify the values of first-level pointer variables and their corresponding memory units through multilevel pointers, as shown in the following examples:

void mian()
{
	int a=10;
	int b=20;
	const int* const p=&a;;
	int** p=&p;
	*p=&b
	printf("%d\n",*p);		//Output is 20
	**p=100;
	printf("%d\n";*p);	//Output is 100
}

3. Pointer and Array
Array is a constant and assignments are not allowed
The value of the array name is the address of the first element of the array
Note: Arrays as function parameters degenerate to pointers, losing array precision (that is, losing array size)
For example:

void main()
{
	int arr[]={1,2,3,4,5,6,'a'};
	//arr=100; 	// Errors will occur
	int* p;
	p=arr;
			//Difference between pointer and array
			//p is a variable, arr is a constant
			//p is a 4-byte pointer and arr is a 40-byte address constant
				//Pointer plus 1, equivalent to pointing to the next element of the array
	printf("%d\n",p);	//The printed value is the same as below
	printf("%d\n",arr);	//Printed values are the same as above
	for(i=0;i<7;i++)
	{
		printf("%d\n",arr[i]);	
		printf("%d\n",p[i]);
		printf("%d\n",*(arr+i));	//Three printing methods print the same result* (arr+2)-->arr[2]
	}
}

Pointer addition or subtraction is related to pointer type
Pointer type variable plus 1, memory address deviation is 4, pointer offset = memory address deviation / bytes occupied by pointer type (sizeof(int))
For example, p+=1; Then the address is from 0xff00 ->0xff04

4. Pointer Array: The element type of the array is the pointer type, which is a special two-dimensional array model

Example 1: Array contains constant element address

void main()
{
	int a=1;
	int b=2;
	int c=3;
	int* arr[3]={&a,&b,&c};	//The elements in the array are all addresses
	for(i=0;i<3;i++)
	{
		printf("%d/t",*arr[i]);
	}
}

Example 2: Array contains the address of an array element (equivalent to a two-dimensional array)

void main()
{
	int a[]={1,2,3};
	int b[]={4,5,6};
	int c[]={7,8,9};
	int* arr[]={a,b,c};
	for(int i=0;i<3;i++)
	{
		for(int j=0;j<3;j++)
		{
			printf("%d ",*arr[i][j]);
			printf("%d ",*(arr[i]+j));
			printf("%d ",*(*(arr+i)+j));	//All three printing methods are possible
		}
		puts(" ");
	}
}


5. Multi-level pointer: that is, the pointer of the pointer....
The first-level pointer is used more, the second-level pointer is used less, and the third-level pointer and above are used less.
Sample code:

void main()
{
	int a=10;
	int b=20;
	int* p=&a;
	int* *pp=&p;	//The secondary pointer variable corresponds to the address of the primary pointer variable
	int** *ppp=&pp;	//The third-level pointer variable corresponds to the address of the second-level pointer variable
		//*ppp==pp=&p
		//**ppp==*pp==p=&a
		//***ppp==**pp==*p==a
	
	*pp=&b	//Remove the secondary pointer variable corresponding to the first pointer variable p, and assign the address of B to the first pointer variable P. Equivalent to p=&b
	**pp=100;
	//*pp=100;//err
	printf("%d\n",*p);
	printf("%d\n",a);
}

6. Pointer function: Pointer as function parameter
Example:

void swap(int* a,int* b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
}
void main()
{
	int a=10;
	int b=20;
	swap(&a,&b);	//Address-passing parameters can change the value of an argument. If they are value-passing, they cannot change the value of an argument.
	printf("%d\n",a);
	printf("%d\n",b);
}

7. Pointer array name as function parameter
Example:

void mystracat(char* ch1,char* ch2)
{
	while(*ch1)ch1++;
	while(*ch2)
	{
		*ch1=*ch2;
		ch1++;
		ch2++;
	}
		//while(*ch1++=*ch2++) equals
		//if(*ch1++==*ch2++)
		//{ch1++;ch2++;}
}

8. Pointer as function return value
Example:

char* my_atrchr(char* str,char ch)	//Note that the definition of the function is pointer type, and the fixed return value should be an address
{
	int i=0;
	while(str[i])
	{
		if(str[i]==ch)
		{
			return &str[i];
		}
		i++;
	}
	return NULL;
}

9. Pointer and string

void main()
{
	char ch[]="hello world";	//Stack String can be modified
	char* p="hello world";	//	Data area constant area string cannot be modified
	ch[2]='m';
	printf("%s",ch);	//Can be modified, the modified value is hemlo world
	//p[2]='m';
	//*(p+2)='m'; // Pointer cannot be modified
}

10. Character pointer as function parameter
Example:

int my_strlen(char* ch)
{
	//Calculates the effective length of a string
	int i=0;
	while(ch[i]!='\0')
	{
		i++;
	}
	return i;
}

Or:

int my_strlen(char* ch)
{
	//Calculate String Length
	char* temp=ch;
	while(*temp)temp++;
	return temp=ch
}

Summary:
Figure

Character string


Article uploaded to

http://blog.bools.cn/archives/167

Keywords: C pointer string Functional Programming

Added by ICEcoffee on Wed, 19 Jan 2022 19:44:17 +0200