C language is not commonly used functions Usage Summary - Long-term update

Although not commonly used, if used to find them much more convenient.

1.memset function: assign the same value for each element of the array (the assignment byte - byte each assigned the same value)

Memset (array name, value, sizeof (array name))
* void * Memset (void , int, size_t);
Indispensable string.h header file.
Recommended assignment: 0 / -1. Because memset function is a byte assignment, the full complement of 0 is 0, 1 full complement -1, less error prone.

#include"stdio.h"
#include "string.h"

int main() {
    printf("赋值前:");
    int a[5] = {1, 3, 5, 10, 7};

    for (int i = 0; i < 5; i++)
        printf("%d ", a[i]);
    memset(a, -1, sizeof(a));
    printf("\n赋值后:");
    for (int i = 0; i < 5; i++)
        printf("%d ", a[i]);
}

Test results:
Here Insert Picture Description

Published 33 original articles · won praise 1 · views 4164

Guess you like

Origin blog.csdn.net/qq_39827677/article/details/103786049