0

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));
}

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, never thought it was that simple
sizeof(directory[0]) would also work in the same way. To keep it as similar to what you asked in the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.