Dave
Dave

Reputation: 474

Filling a 2D array without a for loop in C

I've been trying to fill a 10X10 int array with the number 46 and want to do it in one line, with out the use of a nested forloop. I've been trying to use memset but a doubt I'm using it for the right purpose. Can I do this in one line?

Kind Regards, David

Upvotes: 0

Views: 249

Answers (1)

DWilches
DWilches

Reputation: 23015

As you say it is an int array then you cannot use memset, as if you do:

memset (my_ptr, 46, 10*10);

Then you will be writing the value 46 in the 100 first bytes of your array, and if each int occupies 4 bytes then you are just assigning a weird value to the 25 first ints.

If you are in C then a single for loop is enough, no need to nest.

Upvotes: 1

Related Questions