error: expression must have pointer-to-object type

SYM2RR

I tried to design a program that will return the original 32bit value w but change the number i element to 1. Here's the function I got so far. But for this part, v[i]=1; , it just says that for i expression must have pointer to object type.

 unsigned int setBit(unsigned int w,unsigned int i)
 {
    unsigned int v = w;
    v[i]=1;
    return v;
 }
4pie0
unsigned int v = w;
v[i] = 1; // error, v is not an array

This is not correct because v is not an array. The solution might be using a std::bitset or simply shifting bits and using some bit manipulation - this would be faster.

unsigned int setBit(unsigned int w,unsigned int i) {
    unsigned int v = ( w |= 1 << i);
    return v;
}

usage:

int main(int argc, char *argv[])
{
    unsigned int in = 0;
    unsigned int res = setBit(in,1); // set 1st bit in 0 to 1, results in 2
    return 0;
}

meaning of unsigned int v = ( w |= 1 << i);

| - the bitwise OR

<< - the bitwise shift

v = ( w |= 1 << i) is the same as v = ( w = w | 1 << i) so it means: v is equal to (take w and OR it with 1 left shifted by i, and assign this to w)

about C/C++ bit manipulation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Expression must have pointer type error on a pointer?

Expression must have a pointer-to-object type error in an array

the expression must have an object pointer type

What this error mean : expression must have pointer to object type but it has type struct add_stock in c

Expression must have pointer to object type ERROR while executing C Program in Visual Studio

Expression must have pointer-to-class-type error

When does error "expression must have pointer type" occur?

Solving a "expression must have pointer to class type" error

Error: expression must have pointer-to-class type

Expression must have arithmetic type error with float pointer

Pointer to a Vector of Vectors in a Function Gives 'expression must have pointer type' error

"Expression must be a pointer to a complete object type"

MSVC - expression must have pointer-to-object type but it has type "float" on generic array?

c++ "for" iterator. Expression must have pointer to class type but it has type "object*

error: expression must have integral or enum type

Error. Expression must have a class type

error: expression must have a class type

error : expression must have class type

Error - "Expression must have class type"

Error(E0852)expression must be a pointer to a complete object type(generic linked list)

During manual calling the destructor occurs the error: expression must be a pointer to a complete object type

Expression must be a pointer to a complete object type, why do I get this error in this situation?

expression preceeding of apparent call must have pointer-to func type

Expression must have pointer-to-struct-or-union type?

function pointer - Expression preceding parentheses of apparent call must have (pointer-to-) function type

Using "this" pointer in template class function definition -- expression must have (pointer-to-) function type

(Java) The type of the expression must be an array type but it resolved to Object error

Error: "expression must have integral or unscoped enum type"

C++ / Error: expression must have integral or unscoped enum type