1. Introduction

C language functions are most commonly used in pointer parameters and return address, especially in string processing, often need to encapsulate various functions to complete data processing, and the C language standard library also provides a string. H header file, which contains a lot of string processing functions; The arguments and return values of these functions are almost always pointer types. This article shows you how to use Pointers as function parameters and as function return values.

Here are some sample code to demonstrate usage.

2. Modify the value of the local variable space in the main function indirectly through Pointers

#include <stdio.h>
#include <string.h>
void func(int *p);
int main(a)
{
    int a=100;
    func(&a);
    printf("a=%d\n",a); / / 200
    return 0;
}
​
void func(int *p)
{
    printf("p=%d\n",*p);
    *p=200;
}
Copy the code

3. Use pointer types to swap local variables in main indirectly

#include <stdio.h>
#include <string.h>
void func(int *a,int *b);
int main(a)
{
    int a=100,b=200;
    func(&a,&b); / / func (100200).
    printf("a=%d,b=%d\n",a,b);
    return 0;
}
​
void func(int *a,int *b)
{
    int c;
    c=*a;
    *a=*b;
    *b=c;
}
The return statement can return only one value
// If a function wants to return multiple values, it can use Pointers (parameters).
Copy the code

4. The function returns a pointer type

#include <stdio.h>
#include <string.h>
char *func(char *p);
​
int main(a)
{
    char str[]="1234567890";
    char *p;
    p=func(str);
    printf("%s\n",p); //
    return 0;
}
​
char *func(char *p)
{
    p+=3; //p is offset back by 3 bytes
    return p;
}
Copy the code

5. Write a function that calculates the length of a string

The string () function returns the length of a string.

The function is the same as strlen.

#include <stdio.h>
#include <string.h>
int my_strlen(char *p); // Function declaration
int main(a)
{
    char buff[100];
    int len;
    
    printf("Please enter the string :");
    scanf("%s",buff);
    
    len=my_strlen(buff);
    printf("len=%d\n",len);
    return 0;
}
​
// Each function should have a single function// Calculates the length of the string. Character string ends with '\0'
/ / "1234567890"
int my_strlen(char *p)
{
    char *str=p;
    while(*str++! ='\ 0') {}return str-p- 1;
}
Copy the code

6. Write a string sort function

Function function: pass a string, in the function to achieve the string from large to small or small to large order.

Note: Sorting from large to small or from small to large can be distinguished by function parameters.

#include <stdio.h>
#include <string.h>
int my_strlen(char *p); // Function declaration
void str_sort(char *p,char flag);// Function declaration
int main(a)
{
    char buff[100];
    int len;
    
    printf("Please enter the string :");
    scanf("%s",buff);
    
    // Sort
    str_sort(buff,0);
    printf("Sort from smallest to largest =%s\n",buff);
    str_sort(buff,1);
    printf("Sort from largest to smallest =%s\n",buff);
    return 0;
}
​
// Each function should have a single function// Calculates the length of the string. Character string ends with '\0'
/ / "1234567890"
int my_strlen(char *p)
{
    char *str=p;
    while(*str++! ='\ 0') {}return str-p- 1;
}
​
/* Function function: string sort. Char *p Specifies the character string to be sorted. Char Flag Specifies the sorting method. 0 indicates that the sorting is from smallest to largest
void str_sort(char *p,char flag)
{
    int len;
    int i,j,tmp;
    len=my_strlen(p);
    for(i=0; i<len- 1; i++) {for(j=0; j<len- 1-i; j++) {if(flag)//1 indicates the order from largest to smallest
            {
                if(p[j]<p[j+1])
                {
                    tmp=p[j];
                    p[j]=p[j+1];
                    p[j+1]=tmp; }}else//0 indicates the order from smallest to largest
            {
                if(p[j]>p[j+1])
                {
                    tmp=p[j];
                    p[j]=p[j+1];
                    p[j+1]=tmp;
                }
            }
        }
    }
}
​
//unsigned char a; // 0 to 255 for 1 =8 bits 1111 11111
//char a; / / - 128 ~ + 127
Copy the code

7. Default system initialization value of a variable

#include <stdio.h>
#include <string.h>
int a1;
int main(a)
{
    int a2;
    static int a3;
    printf("a1=%d\n",a1); / / 0
    printf("a2=%d\n",a2); / / random value
    printf("a3=%d\n",a3); / / 0
    return 0;
}
/* The default value for global variables is 0. The default value for local variables is random (the system does not assign a value). The default value for static variables is 0
Copy the code