1. Introduction

Void is an empty type in the basic data type.

Void is often used as the return value of a function. The function parameter declaration means that the function has no return value and no parameters.

The void type cannot be used to define a variable, because it is an empty type — an empty type.

void abc;  // This is wrong
Copy the code

Void * is a universal pointer type that can be converted to any pointer type, especially when used in memory copy.

2. The memory copy function memcpy

The system provides functions: memory copy function

#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
Copy the code

Example code: memcpy function procedure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main(int argc,char **argv)
{
    char str1[]="1234567890";
    char str2[100];
    int int_a[]={1.2.3.4.5.6.7.8.9.0};
    int int_b[10];
    int a=200;
    int b;
    int i;
    // Copy an array of strings
    memcpy(str2,str1,sizeof(str1));
    printf("str1=%s\n",str1);
    printf("str2=%s\n",str2);
    // Copy an integer array
    memcpy(int_b,int_a,sizeof(int_a));
    for(i=0; i<10; i++) {printf("%d ",int_a[i]);
    }
    printf("\n");
    for(i=0; i<10; i++) {printf("%d ",int_b[i]);
    }
    printf("\n");
    // Copy variables
    memcpy(&b,&a,sizeof(a));
    printf("a=%d\n",a);
    printf("b=%d\n",b);
    
    return 0;
}
Copy the code

3. Write memcpy functions by yourself

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void my_memcpy(void *p1,const void *p2,int cnt);
int main(int argc,char **argv)
{
    char str1[]="1234567890";
    char str2[100];
    int int_a[]={1.2.3.4.5.6.7.8.9.0};
    int int_b[10];
    int a=200;
    int b;
    int i;
    // Copy an array of strings
    my_memcpy(str2,str1,sizeof(str1));
    printf("str1=%s\n",str1);
    printf("str2=%s\n",str2);
    // Copy an integer array
    my_memcpy(int_b,int_a,sizeof(int_a));
    for(i=0; i<10; i++) {printf("%d ",int_a[i]);
    }
    printf("\n");
    for(i=0; i<10; i++) {printf("%d ",int_b[i]);
    }
    printf("\n");
    // Copy variables
    my_memcpy(&b,&a,sizeof(a));
    printf("a=%d\n",a);
    printf("b=%d\n",b);
    return 0;
}
​
/* Memory copy function */
void my_memcpy(void *p1,const void *p2,int cnt)
{
    char *str1=(char*)p1;
    char *str2=(char*)p2;
    int i;
    for(i=0; i<cnt; i++) { *str1=*str2; str1++; str2++; }}Copy the code

4. Memory initialization function: memset

The memset function is often used to initialize a memory space by assigning an initial value to the space.

#include <string.h>
//void *memset(void *s, int c, size_t n);
// Assigns the specified space to the specified value.#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char **argv)
{
    int int_b[10];
    int b;
    int i;
    //memset(void *s, int c, size_t n);
    memset(int_b,0.sizeof(int_b));
    // Copy an integer array
    for(i=0; i<10; i++) {printf("%d ",int_b[i]);
    }
    printf("\n");
    // Copy variables
    memset(&b,0.sizeof(int_b));
    printf("b=%d\n",b);
    return 0;
}
Copy the code

5. Write your own memset

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void my_memset(void *p,int c,int n);
int main(int argc,char **argv)
{
    int int_b[10];
    int b;
    int i;
    //memset(void *s, int c, size_t n);
    my_memset(int_b,0.sizeof(int_b));
    // Copy an integer array
    for(i=0; i<10; i++) {printf("%d ",int_b[i]);
    }
    printf("\n");
    // Copy variables
    my_memset(&b,0.sizeof(int_b));
    printf("b=%d\n",b);
    return 0;
}
​
void my_memset(void *p,int c,int n)
{
    char *str=(char*)p;
    int i;
    for(i=0; i<n; i++) { *str=c; str++; }}Copy the code

6. Read and write int data to a file

In file reading and writing, you often need to read and write custom data to a file.

#include <stdio.h>
#include <string.h>
int write_file(a);
int read_file(a);
​
int main(int argc,char **argv)
{
    write_file(a);read_file(a);return 0;
}
​
int write_file(a)
{
    int cnt;
    int a=123;
    /*1. Open file */
    FILE *fp=fopen("test.txt"."wb");
    if(fp==NULL)
    {
        printf("Failed to open file.\n");
        return - 1;
    }
    /*2. Write data */
    cnt=fwrite(&a,1.sizeof(a),fp);
    printf("Write %d bytes successfully",cnt);
    /*3. Close file */
    fclose(fp);
    return 0;
}
​
int read_file(a)
{
    int cnt;
    int a;
    /*1. Open file */
    FILE *fp=fopen("test.txt"."rb");
    if(fp==NULL)
    {
        printf("Failed to open file.\n");
        return - 1;
    }
    /*2. Read data */
    cnt=fread(&a,1.sizeof(a),fp);
    printf("Read %d bytes \n successfully",cnt);
    printf("a=%d\n",a);
​
    /*3. Close file */
    fclose(fp);
    return 0;
}
Copy the code