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
Solved

Explanation on compiler error needed.

Posted on 2003-11-16
5
1,114 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
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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stack 22 171
What technology should I use for communication Raspberry Pi 2b and LapTop java application 2 134
Problem to show menu 5 93
An API detour question 7 93
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…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

860 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