C language - inline function, recursive function, pointer function

Posted by lyonsperf on Fri, 10 Dec 2021 14:05:55 +0100

1. Preface

This article introduces the knowledge points of C language, such as inline function, recursive function, function pointer, pointer function, local address, const keyword, extern keyword and so on; These knowledge points are very common and important in actual project development.

The following describes each knowledge point in the process of a small chapter.

2. Address problem of function return local space

Subfunction: after the call, the space will be released - reclaimed by the system.

Summary: a subfunction cannot return the address of a local variable.

Example 1:

#include <stdio.h>
char *func(void);
int main()
{
	printf("%s\n",func());  //It won't print out.
	return 0;
}

char *func(void)
{
	char buff[]="1234567890";
	return buff;
}

Example 2:

#include <stdio.h>
char *func(char *p);
int main()
{
	char buff[]="1234567890";
	printf("%s\n",func(buff)); //Can print
	return 0;
}

char *func(char *p)
{
	return p;
}

Example 3:

#include <stdio.h>
char *func(void);
int main()
{
	printf("%s\n",func());  //Can print
	return 0;
}

char *func(void)
{
	static char buff[]="1234567890";
	return buff;
}

3. const read-only keyword (constant)

(1) const keyword -- decorated variable

#include <stdio.h>
int main()
{
	//const int a; // If initialization does not assign a value, this line of code is meaningless
	const int a=100;
	a=200; //Bad code -- cannot assign to constant -- read only variable assignment
	printf("a=%d\n",a);
	return 0;
}

(2) const keyword -- decorated pointer

#include <stdio.h>

//Pointer: data field, pointer (address) field

int main()
{
	int a=100;
	int b=200;
	//const is commonly used in four ways to modify pointers
	const int *p1=&a; //The pointing space value cannot be modified. The pointing address can be changed
	int const *p2=&a; //The pointing space value cannot be modified. The pointing address can be changed
	int *const p3=&a; //The pointing space value can be modified, and the pointing address cannot be changed
	const int *const p4=&a;  //The value to the space cannot be modified, and the address pointed to cannot be changed
	//int *p5 const;  The grammar is wrong
	
	//*p1=666;  FALSE
	//p1=&b;    correct
	//*p2=666; FALSE
	//p2=&b; correct
	//*p3=666; correct
	//p3=&b; FALSE
	//p4=&b; FALSE
	//*p4=666; FALSE
	return 0;
}

4. Inline function

Inline function: when calling, the stack will not be pushed out of the stack (it will not go through the process of saving the address and recovering the address).

Inline functions are equivalent to a replacement process.

Attention should be paid to the design of inline functions: inline functions can only write simple code - not complex code.

The local variables in the function are stored in the stack space. Stack space: it is managed by the system.

#include <stdio.h>
void func(void);
int main()
{
	int a;
	func();
	printf("12345\n");
	return 0;
}

//Inline declaration - defines inline functions
inline void func(void)
{
	printf("hello\n");
}

5. External reference declaration

extern is mostly used for reference declarations of variables, functions and other data types in multi file programming.

When an external reference is declared, it cannot be assigned.

#include <stdio.h>
//Reference declaration
extern int a;
extern char buff[];
extern void func();

int main()
{
	printf("%d\n",a);
	printf("%s\n",buff);
	func();
	return 0;
}

int a=100;
char buff[]="1234567890";
void func()
{
	printf("hello\n");
}

6. Definition of string array and string constant

#include <stdio.h>
int main()
{
	//The string pointed to by p1 is assigned when the program is compiled
	char *p1="1234567890"; //Pointer to string constant
	//p2 array is assigned when the program is running
	char p2[]="abcdefg";
	//p1[0]='A';  FALSE
	printf("%s\n",p1);
	printf("%s\n",p2);
	return 0;
}

Example 2:

#include <stdio.h>

int main()
{
	//The string pointed to by p1 is assigned when the program is compiled
	char *p1="1234567890"; //Pointer to string constant
	//p2 array is assigned when the program is running
	char p2[]="abcdefg";
	int a;
	int b;
	int c;
	printf("a=%#x\n",&a);
	printf("b=%#x\n",&b);
	printf("c=%#x\n",&c);
	printf("p1=%#x\n",p1);
	printf("p2=%#x\n",p2);
	return 0;
}
/*
a=0xbf9f93e0
b=0xbf9f93dc
c=0xbf9f93d8
p1=0x8048524
p2=0xbf9f93e4
*/

7. Standard main function parameter syntax

#include <stdio.h>
/*
int argc :Number of parameters passed in (including the executable itself)
char **p :Save incoming data address
main The parameter data passed in is of string type.
Parameter transmission method:/ a.out 123 456 789
*/
int main(int argc,char **p)
//int main(int argc,char *p[]) p[0] p[1] p[2]
{
	int i;
	for(i=0;i<argc;i++)
	{
		printf("p[%d]=%s\n",i,p[i]);
	}
	return 0;
}

8. Pointer function and function pointer

Array pointer: itself is a pointer to a two-dimensional array (one-dimensional array pointer). int (*p)[5];

Pointer array: itself is an array, and the address is stored in the array. int *p[5]; (equivalent to 5 pointers defined)

The name of the array itself is the first address of the array element - the array name is the address.

**Function pointer: * * itself is a pointer, a pointer to a function. Syntax: int (*p)(int,int); The + + and - operators are not supported.

Pointer function: itself is a function, indicating that the return value of the function is a pointer type. Syntax: int *func(int a,int b) {}

The function name is the address.

Example 1:

#include <stdio.h>
int func(int a,int b);
int main(int argc,char **argv)
{
	int (*p)(int,int); //Pointer to function
	p=func;
	printf("%d\n",func(10,20)); //Call function by function name
	printf("%d\n",p(10,20)); //Calling a function through a pointer -- Writing 1
	printf("%d\n",(*p)(10,20)); //Calling a function through a pointer -- writing 2
	return 0;
}

int func(int a,int b)
{
	return a+b;
}

Example 2: function pointer as function parameter

#include <stdio.h>
int func1(int a,int b);
int func2(int (*p)(int,int),int a,int b);

int main(int argc,char **argv)
{
	printf("%d\n",func2(func1,100,200));
	return 0;
}

int func1(int a,int b)
{
	return a+b;
}

int func2(int (*p)(int,int),int a,int b)
{
	return p(a,b);
}

9. Recursive function

What is a recursive function? A subroutine that directly or indirectly calls its own procedure is called recursion.

The function calls its own procedure - recursion.

Precautions for recursive function: there must be a termination condition.

Example 1:

#include <stdio.h>
int func(int a);
int main(int argc,char **argv)
{
	printf("%d\n",func(1)); //10
	return 0;
}

int func(int a)
{
	a++;
	if(a==10)
	{
		return a;
	}
	else
	{
		func(a);
	}
}

Example 2: calculating the length of a string - using recursion

#include <stdio.h>
int func(char *p);
int main(int argc,char **argv)
{
	printf("%d\n",func("1234567890")); //10
	return 0;
}

//Calculate string length
int func(char *p)
{
	if(*p=='\0')
	{
		return 0;
	}
	return 1+func(p+1);
}

/*
Demonstrate the return process of recursive functions:
a(); //3

int a()
{
	
	return 1+b();
}

int b()
{
	return 1+c();
}

int c()
{
	return 1;
}
*/

Topics: C C++