gcc 4.4.4 c89
Pointers are not the same as arrays. But arrays can decay into pointers.
I was just using memset which first parameter is a pointer. I would like to initialize my structure array.
i.e.
struct devices
{
char name[STRING_SIZE];
size_t profile;
char catagory;
};
struct devices dev[NUM_DEVICES];
memset(dev, 0, (size_t)NUM_DEVICES * sizeof(*dev));
dev == &dev[0]
But should I pass the first parameter has this:
memset(&dev, 0, (size_t)NUM_DEVICES * sizeof(*dev));
Many thanks for any advice,
What you have:
is fine – you pass a pointer to the first element of the array, and the size of the array. However, the
(size_t)cast is unnecessary (sizeofhas typesize_t, so it will cause the correct promotion) and I find thatdev[0]is clearer than*devin this case:Alternatively, you can use
&devas the address. In this case, it is probably clearer to usesizeof dev– the size of the whole array:I say that this is clearer, because it’s generally best to have the first parameter be a pointer to the type that’s the subject of
sizeofin the last parameter: thememset()should look like one of these forms:Note however that this last one only works if
devreally is an array – like it is in this case. If instead you have a pointer to the first element of the array, you’ll need to use the first version.