C语言内存函数

C语言 memset() 函数用于将内存块中填充某个指定的值

语法

void *memset(void *s, int c, size_t n);

    参数

    参数说明
    s
    指定要填充的内存块。
    c指定内存块中要填充的值。
    n

    指定内存块中要填充的字符数。sizeof(int) 或者 size(char)

    返回值

    没有返回值,改变原内存的存储区域内容。

    例子

    介绍一些例子,了解c语言memset()函数的使用方法。

    #include <stdio.h>
    #include <string.h>
    int main(){
        char s[]="yxcjc123";
        int i;
    	
        printf("填充前:%s\n",s);
        memset(s,'y', 3*sizeof(char));//填充前面3个为'y'
    
        printf("填充后:%s\n",s);
       
        getchar();
        return 0;
    
    
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    输出:

    C语言memset()函数