?
Solved

Explanation on compiler error needed.

Posted on 2003-11-16
5
Medium Priority
?
1,176 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

764 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