I have a function that passes in a struct that and instead of doing bit manipulations on the arr itself I want to create copy. How can I make a copy of an element of an array of unsigned ints to do bit manipulations on?
unsigned int * arr = cs->arr; // cs->arr is set as unsigned int * arr;
unsigned int copy;
memcpy(copy,arr[0], sizeof(unsigned int)); // Copy into copy the fir开发者_Go百科st element, for now
int i = 0;
while(copy != 0)
{
i += copy & 1;
copy >>= 1;
}
return i;
Thank you!
You dont need memcopy
. A simple array access is enough:
unsigned int copy = cs->arr[0];
int i = 0;
while(copy != 0)
{
i += copy & 1;
copy >>= 1;
}
return i;
copy = arr[0];
is all that's needed. copy
will have the same value as arr[0]
, but it won't be linked to it in any other way. (i.e. modifying copy
will not change arr[0]
.)
精彩评论