memset() for two dimensional arrays

How to use memset() for two dimensional arrays?

I tried using
 
memset(arr,1,sizeof(arr)*m*n);

(m and n are the length and breadth of the array arr respectively).
But,it doesn't seem to work.The values weren't set when I checked.
1. Show the declaration of 'arr'.

2. Why memset()? What are you actually trying to achieve?
If arr really is a two dimensional array and not a pointer you can get the size in bytes by doing sizeof(arr).
@keskiverto
int arr[m][n];
I had to set all the values before I could use it for a problem.
I had to set all the values

... what number were you trying to assign? Was it 16843009?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

int main()
{
    int a = 0;

    memset( (void *) &a, 1, sizeof(a) );
    
    cout << "a hex: "   << hex << a 
         << "  a dec: " << dec << a << endl;    
}


output on my 32-bit system:
a hex: 1010101  a dec: 16843009



memset() is used to initialize raw memory. If you want to initialize an array as its data type, use std::fill(). For example,
1
2
const size_t length = sizeof(arr) / sizeof(arr[0][0]);
std::fill(arr, arr + length, 1);
Topic archived. No new replies allowed.