markh
markh

Reputation: 35

Segmentation fault when memset struct array in c

I am trying to empty all data in the struct array.

but it turns out to be segmentation fault. Can someone please help me.

Struct

struct entry
{
   char  user;
    char  name[9];
    char  extension[4];
    short blockcount;
    short block[8];
};
struct entry directory[128];

main()

for (int i = 0; i < 128; ++i)
{
      memset(&directory[i], 0, sizeof(directory));
}

Upvotes: 0

Views: 695

Answers (1)

Pras
Pras

Reputation: 4044

You need to change

memset(&directory[i], 0, sizeof(directory));

to

memset(&directory[i], 0, sizeof(struct entry));

as you want to memset single element of array of structure

To memset entire arry you can also use

memset(directory, 0, sizeof(directory));// single statement, no need to loop all elements

Upvotes: 5

Related Questions