Solved

Explanation on compiler error needed.

Posted on 2003-11-16
5
1,033 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
5 Comments
 
LVL 22

Expert Comment

by:grg99
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scanf and assignment suppression 13 142
memory mapped I/O query 6 136
IDE C compiler 5 119
Convert image to byte array 8 173
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now