Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Explanation on compiler error needed.

Posted on 2003-11-16
5
Medium Priority
?
1,203 Views
Last Modified: 2012-08-13
I consider myself a mediocre C/C++ programmer but I just saw a line of code that stumped me. Anyone who can provide an answer is greatly appreciate.

This is the code

unsigned char DataBuffer[80];
unsigned char *BufferPtr;

.....

BufferPtr = (DataBuffer + 4) & (~0x3);

.....

This is the error "error C2296: '&' : illegal, left operand has type 'unsigned char *'"

Why is this kind of pointer manipulation not allowed?


0
Comment
Question by:simbo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 9761061
What they're trying to do is to align BufferPtr to the next multiple of 4 address.  

The problem might be that on your computer the constant ~3 might not be the same width as a pointer.
You could probably fix this by casting everything to to a unsigned char *:

typedef  unsigned char * ucp;

    BuferPtr = (ucp) (    (ucp)(  DataPtr+3 )   &   ( (ucp)(~3) )    )

A couple other points:

Once the pointer is aligned, we've lost anywhere from zero to three bytes of the array size.
So anything you later do with that array had better not use more than 77 bytes!


0
 

Author Comment

by:simbo
ID: 9761133
Thanks for the response grg99. I've tried that already before posting the question and the compiler responded with the same error message.

The error message I got was

"error C2296: '&' : illegal, left operand has type 'unsigned char *'
error C2297: '&' : illegal, right operand has type 'unsigned char *'"

I've used both ARM compiler and visual C++ and both compilers gave me the same kind of error. "..... '&':illegal left operand ........"

Any other ideas? Thanks
0
 
LVL 15

Accepted Solution

by:
efn earned 2000 total points
ID: 9761422
You are getting the error because the operands of a bitwise operator like & must be integral and a pointer is not integral.  (That is, it's not some kind of integer.)

You will have to convert the pointer to an integer to use the bitwise operator, and then convert the integer to a pointer to assign it to a pointer variable.  For example:

BufferPtr = (unsigned char*)((unsigned long)(DataBuffer + 4) & (~0x3));

--efn
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 9761529
Try out the following.....it will work!
BufferPtr = (unsigned char *) ( ((int)dataBuffer + 4) & (~0x3) );

The problem was due to the array name in the operator list for bitwise (&) operator. Array name will be converted to an address and this cannot be given as an operator for bitwise-operators.....it always expects an int type of operator. So, convert this to int by typecasting to int.
But, the result has to be given to a pointer and this pointer is of type unsigned char. So, you have to typecast it to the pointer of that type and while doing this, the entire expression must be placed within the braces!
Hope you understood why it was giving error before...!?
0
 

Author Comment

by:simbo
ID: 9761549
Thank you everyone for their help. I accepted the answer from efn because his is  the most complete and was posted the earliest
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question