asir
asir

Reputation: 15

initializing a value to a structure

  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

Upvotes: 0

Views: 667

Answers (6)

Jens Gustedt
Jens Gustedt

Reputation: 78903

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.

Upvotes: 1

Matt Joiner
Matt Joiner

Reputation: 118470

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.

Upvotes: 2

Ashish
Ashish

Reputation: 1567

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

Upvotes: -1

Brian Roach
Brian Roach

Reputation: 76888

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

Upvotes: 2

Prasoon Saurav
Prasoon Saurav

Reputation: 92854

Use memset in C

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

Upvotes: 0

Pierre Bourdon
Pierre Bourdon

Reputation: 10880

You can use memset:

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

See the manual page of the memset function.

Upvotes: 1

Related Questions