开发者

how to use memset for double dimentional array?

开发者 https://www.devze.com 2023-02-12 15:53 出处:网络
I have a Double dim. array: alarm_1_active_buffer[MAX_NUM_ALARMS][MAX_ALARM_STRING_SIZE]; I want to clear the buffer before filling it.

I have a Double dim. array:

 alarm_1_active_buffer[MAX_NUM_ALARMS][MAX_ALARM_STRING_SIZE];

I want to clear the buffer before filling it.

Like this :

 for(index=0; index<MAX_NUM_ALARMS ; index++)
    {
        memset(&alarm_1_active_buffer[index], 0, MAX_ALARM_STRING_S开发者_如何学运维IZE);
        memset(&alarm_1_active_buffer[index],string, MAX_ALARM_STRING_SIZE);
    }

It is not working.


Since the arrays are laid in continuos address spaces, you don't have to do anything special for 2d arrays. You can simply use memset(alarm_1_active_buffer, 0, MAX_NUM_ALARMS * MAX_ALARM_STRING_SIZE * sizeof(type of alarm_1_active_buffer));.


Making sure to #include <string.h> first:

memset(alarm_1_active_buffer, 0, sizeof(alarm_1_active_buffer));

This method works regardless of the type of elements in the array.

0

精彩评论

暂无评论...
验证码 换一张
取 消