how to use memset for double dimentional array?

一世执手 提交于 2019-12-25 06:40:07

问题


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_SIZE);
        memset(&alarm_1_active_buffer[index],string, MAX_ALARM_STRING_SIZE);
    }

It is not working.


回答1:


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));.




回答2:


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.



来源:https://stackoverflow.com/questions/5089043/how-to-use-memset-for-double-dimentional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!