dali1985
dali1985

Reputation: 3313

Clear a 2d char array

I would like to ask if there is any fast way(using memset for example) to clear a char table like

char mytable[2][10];

For example as I know using memset

memset(mytable, 0, sizeof(mytable));

is used for 1D tables.

Upvotes: 0

Views: 878

Answers (3)

user2107435
user2107435

Reputation:

you can use pointer :

register int i ;

const int max = MAXROW * MAXCOL ;

char *p = &mat[0][0] ;

for (i=0; i < MAXROW * MAXCOL ; i++)

*p++ = 'x' ;

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

What you have written will work for 2-D table as well. Actually it will work for any dimension of the array(assuming you are memset-ing a static array)

Upvotes: 0

user529758
user529758

Reputation:

They're called arrays, not "tables".

And the very same code works for any array with arbitrary number of dimensions.

Upvotes: 5

Related Questions