Reputation: 107
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
struct name{
char array[8];
};
struct name na;
memset(&na.array,0,8);
}
It is the same memset(&na.array,0,8); that memset(na.array,0,8); ?
and it is the same na.array that &na.array[0] ?
Upvotes: 1
Views: 47
Reputation: 726479
The three expressions that you mention produce identical results when passed as parameters to a function taking void*
. The reasons why it is so are different:
&na.array
is the address of the array. It matches the address of its initial element, but it has a different type. However, you are passing it to a function taking void*
, so the type does not matter.na.array
is the array itself. It "decays" to a pointer to array's initial element when passed to a void*
parameter.&na.array[0]
is the address of array's initial element, taken directly. It gets converted to void*
when you pass it as a parameter to memset
.Upvotes: 1
Reputation: 15229
It is the same memset(&na.array,0,8); that memset(na.array,0,8); ?
Yes.
However, this only means that both variants behave equivalently. &na.array
and na.array
have very different types.
na.array
is of type char[8]
, an array of eight char
s. &na.array
is of type char(*)[8]
, a pointer to an array of eight char
s.
and it is the same na.array that &na.array[0] ?
Strictly speaking, no. na.array
is actually of type char[8]
while &na.array[0]
is of type char *
. However, there is an implicit conversion rule from char[8]
to char *
, which is called decay. So, na.array
can decay to a char *
, which is precisely the type of &na.array[0]
.
Upvotes: 1
Reputation: 17678
It is the same memset(&na.array,0,8); that memset(na.array,0,8); ?
Same thing, and you should use sizeof
instead of hardcode the array size.
memset(&na.array,0,sizeof(na.array));
Personally I would prefer using &
to make the address of the array explicit.
Upvotes: 1