Using memset for integer array in C

前端 未结 10 1620
遇见更好的自我
遇见更好的自我 2020-11-27 02:20
char str[] = \"beautiful earth\";
memset(str, \'*\', 6);
printf(\"%s\", str);

Output:
******ful earth

Like the above use of memset, can we initial

相关标签:
10条回答
  • 2020-11-27 02:56

    Memset sets values for data types having 1 byte but integers have 4 bytes or more , so it won't work and you'll get garbage values. It's mostly used when you are working with char and string types.

    0 讨论(0)
  • 2020-11-27 02:58

    No, you can't [portably] use memset for that purpose, unless the desired target value is 0. memset treats the target memory region as an array of bytes, not an array of ints.

    A fairly popular hack for filling a memory region with a repetitive pattern is actually based on memcpy. It critically relies on the expectation that memcpy copies data in forward direction

    int arr[15];
    
    arr[0] = 1;
    memcpy(&arr[1], &arr[0], sizeof arr - sizeof *arr);
    

    This is, of course, a pretty ugly hack, since the behavior of standard memcpy is undefined when the source and destination memory regions overlap. You can write your own version of memcpy though, making sure it copies data in forward direction, and use in the above fashion. But it is not really worth it. Just use a simple cycle to set the elements of your array to the desired value.

    0 讨论(0)
  • 2020-11-27 03:00

    Actually it is possible with memset_pattern4 (it sets 4 bits at a time).

    memset_pattern4(your_array, your_number , sizeof(youre_array));

    0 讨论(0)
  • 2020-11-27 03:01

    I tried the following program and it appears that you can initialize your array using memset() with -1 and 0 only

    #include<stdio.h>
    #include<string.h>
    
    void printArray(int arr[], int len)
    {
            int i=0;
        for(i=0; i<len; i++)
        {
            printf("%d ", arr[i]);
        }
        puts("");
    }
    
    int main()
    {
        int arrLen = 15;
        int totalNoOfElementsToBeInitialized = 6;
    
        int arr[arrLen];
        printArray(arr, arrLen);
        memset(arr, -1, totalNoOfElementsToBeInitialized*sizeof(arr[0]));
        printArray(arr, arrLen);
        memset(arr, 0, totalNoOfElementsToBeInitialized*sizeof(arr[0]));
        printArray(arr, arrLen);
        memset(arr, 1, totalNoOfElementsToBeInitialized*sizeof(arr[0]));
        printArray(arr, arrLen);
        memset(arr, 2, totalNoOfElementsToBeInitialized*sizeof(arr[0]));
        printArray(arr, arrLen);
        memset(arr, -2, totalNoOfElementsToBeInitialized*sizeof(arr[0]));
        printArray(arr, arrLen);
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 03:05

    Since nobody mentioned it...

    Although you cannot initialize the integers with value 1 using memset, you can initialize them with value -1 and simply change your logic to work with negative values instead.

    For example, to initialize the first 6 numbers of your array with -1, you would do

    memset(arr,-1,6*(sizeof int));
    

    Furthermore, if you only need to do this initialization once, you can actually declare the array to start with values 1 from compile time.

    int arr[15] = {1,1,1,1,1,1};
    
    0 讨论(0)
  • 2020-11-27 03:09

    On Linux, OSX and other UNIX like operating systems where wchar_t is 32 bits and you can use wmemset() instead of memset().

    #include<wchar.h>
    ...
    int arr[15];
    wmemset( arr, 1, 6 );
    

    Note that wchar_t on MS-Windows is 16 bits so this trick may not work.

    0 讨论(0)
提交回复
热议问题