Search code examples
c++memset

Use memset for 2d array at specific row


recently, I have learnt about C++. Specially, I learned about memset function. But I don't know how to set value for 2d array at specific row

Example:

int dp[10][10];

// I want to set all values for dp[0] by using memset
// I can do it, by using For loop, like 
for (int i = 0; i < 10; i++) 
    dp[0][i] = 1000;

I have tried this

memset(dp[0], 1000, sizeof dp[0]);

But it's not working well.

So I want to know if there are any ways to use memset like what I hope? Thanks :D


Solution

  • You can't do such a thing because memset write bytes.
    For instance memset( dp[0],3,sizeof( dp[0] )) ; will write bytes == 3 in all 10 ints of dp[0].
    So dp[i] == 0x03030303 for i in 0..9 !