memset() in C with examples

您所在的位置:网站首页 memset() memset() in C with examples

memset() in C with examples

#memset() in C with examples| 来源: 网络整理| 查看: 265

memset() is used to fill a block of memory with a particular value.The syntax of memset() function is as follows :

// ptr ==> Starting address of memory to be filled // x ==> Value to be filled // n ==> Number of bytes to be filled starting // from ptr to be filled void *memset(void *ptr, int x, size_t n);

Note that ptr is a void pointer, so that we can pass any type of pointer to this function.

Let us see a simple example in C to demonstrate how memset() function is used:

// C program to demonstrate working of memset()#include #include   int main(){    char str[50] = "GeeksForGeeks is for programming geeks.";    printf("\nBefore memset(): %s\n", str);      // Fill 8 characters starting from str[13] with '.'    memset(str + 13, '.', 8*sizeof(char));      printf("After memset():  %s", str);    return 0;}

Output:

Before memset(): GeeksForGeeks is for programming geeks. After memset(): GeeksForGeeks........programming geeks.

Explanation: (str + 13) points to first space (0 based index) of the string “GeeksForGeeks is for programming geeks.”, and memset() sets the character ‘.’ starting from first ‘ ‘ of the string up to 8 character positions of the given string and hence we get the output as shown above.

// C program to demonstrate working of memset()#include #include   void printArray(int arr[], int n){   for (int i=0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3