0
  struct mac_filter
    {
        char ether_dhost[6];
        char ether_shost[6];
        short ether_type;
    };

    struct packet_filter
    {
        struct mac_filter mac;
    };

    main()
    {
    struct packet_filter test;
    test.mac= NULL;
    }

i get

error: incompatible types when assigning to type ‘struct mac_filter’ from type ‘void *’

here i want to initialize all the member function of mac_filter to zero, is there any way to set them to zero instead to initializing each member functions to zero

6 Answers 6

2

Your struct contains an actual struct mac_filter, not a pointer to one. You can't assign NULL to it.

If you wanted a pointer to one, your struct would look like:

struct packet_filter
{
    struct mac_filter *mac;
};

To use it you would need to malloc() memory to it, or assign a pointer to it.

If you just want to zero out your struct, you use memset and pass a pointer to your struct:

memset(&test.mac, 0, sizeof (struct mac_filter));
Sign up to request clarification or add additional context in comments.

2 Comments

if i have structure of 10 member functions how to set a only a few member function of a that structure to zero and leave the others.
Sorry, I don't follow - what is "structure of 10 member functions" ?
2

It's actually faster to memset them all to zero and then fill in the values as they become available later.

However I recommend you use C99 initialization when you know all the values:

struct packet_filter test = {
    .mac = (struct mac_filter) {
        .ether_dhost = {0, 0x11, 0x22, 0x33, 0x44, 0x55},
        .ether_shost = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa};
    },
};

Any values you don't set are automatically initialized to zero. In this case I've left mac.ether_type as 0.

Note that this kind of initialization is very popular with initializing C-style jump tables at both compile time and run time, among other kinds of arrays that are sparsely populated, or index/member fields being set are very specific.

1 Comment

initialization to 0 can and should (almost always) be done by simply using the fall back initializer. At least this is what this question is about as I understand it. Has the advantage to work for most dialects of C, I think.
1

Maybe I don't understand your question correctly but default initialization is doing just what you want:

int main(void)
{
    struct mac_filter test = { 0 };
}

By default that initialization sets all fields recursively to the "correct" form of 0. In most cases this is equivalent to memset but you never know what your target architecture has as specialities.

And if you want to assign to that part (this is something different than initialization) you may use a "compound literal" from C99:

test.mac = (const packet_filter){ 0 };

BTW, the code that you posted is completely bogus. The type of your declaration is not correct and your definition of main is ridiculous. Please post code that is as correct as possible so we can concentrate on the actual problem for the question.

Comments

1

You can use memset:

memset(&test.mac, 0, sizeof (test.mac));

See the manual page of the memset function.

1 Comment

if i have structure of 10 member functions how to set a only a few member function of a that structure to zero and leave the others.
0

Use memset in C

memset(&test.mac, 0, sizeof (struct mac_filter));

3 Comments

if i have structure of 10 member functions how to set a only a few member function of a that structure to zero and leave the others.
@asir : Member functions in C?
@asir : You can't define functions inside structure in C. C and C++ are two different languages.
-1

better you define it as global variable. and default value of global variable is zero.

2 Comments

don't do that. as if the only possibility to initialize a variable by default would be to make it static. Just initialize it. please see my answer.
Global variables should be hunted down and eliminated one by one. They are a very good way to turn any code into an unpredicable mess. There are very few good reasons to use a global variable - this is not one of them.

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.