1

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,

1
  • I know that this isn't exactly your question, but if you just do this to iniitialize your array of struct you should really just use something like the following struct devices dev[NUM_DEVICES] = { { 0 } }; or even better if you have C99 { { .name = "" }} on the right hand side. Commented Aug 13, 2010 at 6:03

1 Answer 1

4

What you have:

memset(dev, 0, (size_t)NUM_DEVICES * sizeof(*dev));

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 (sizeof has type size_t, so it will cause the correct promotion) and I find that dev[0] is clearer than *dev in this case:

memset(dev, 0, NUM_DEVICES * sizeof dev[0]);

Alternatively, you can use &dev as the address. In this case, it is probably clearer to use sizeof dev - the size of the whole array:

memset(&dev, 0, sizeof dev);

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 sizeof in the last parameter: the memset() should look like one of these forms:

memset(p, ..., N * sizeof p[0])
memset(&x, ..., sizeof x)

Note however that this last one only works if dev really 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.

Sign up to request clarification or add additional context in comments.

9 Comments

sizeof x returns number of array elements only if array was allocated statically. Am I right?
@DanSkeel: It doesn't have anything to do with static storage duration - sizeof a will be the size of the array a as long as a is actually an array (and has complete type; it's an error to apply sizeof to something with incomplete type). Note that pointers are not arrays, and applying sizeof to a pointer will give you the size of the pointer. Note also that the size of an array is not necessarily the number of elements, it's the number of elements multiplied by the size of each element.
@caf: Maybe I used a wrong word... By statically I meant allocated on stack. I'm sorry to ask probably obvious thing, but I can't come up with a way to define array variable (not pointer) without setting it's size. So my point is that you can't use sizeof for getting size of dynamically allocated array. Or I miss something? Can you provide example if I'm wrong?
@DanSkeel: How the array is allocated isn't important - the important point is that sizeof will only tell you a size that is part of the type. So you could have a dynamically allocated array like int (*a)[100] = malloc(sizeof *a); where sizeof *a correctly returns the size of the array *a.
Ok, you are right, we can allocate memory on a heap for array of predefined size. But by "dynamically" I meant to allocate array of size that is contained in a variable. If we allocate variable amount of memory we can't define a variable that is array, right? We will have to use pointer, and it will have "incomplete" type that we won't be able to use with sizeof, and will get only size of pointer or size of single element. Too many comments... If you have time I would love to clarify it in chat, thanks. chat.stackoverflow.com/rooms/71457/clarification-of-sizeof-in-c
|

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.