• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Pointer not really an lvalue?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pointer not really an lvalue?


  • Subject: Re: Pointer not really an lvalue?
  • From: "Mark Wagner" <email@hidden>
  • Date: Thu, 13 Jul 2006 12:22:44 -0700

On 7/13/06, Rush Manbert <email@hidden> wrote:
Mark Wagner wrote:
> Under GCC 4, the following code gives a warning "target of assignment
> not really an lvalue; this will be a hard error in the future".  As
> far as I can tell, it's just casting a void * to a char * and
> performing pointer arithmetic on it.  What's the error and how do I
> fix it?
>
> typedef struct SBlockInfo
>     {
>     void* blockPtr;        // points to header of block
>     ASize dataOffset;    // offset from header to data
>     ASize blockSize;    // actual size of block without header
>     ASize usedSize;        // actual used size of block
>     unsigned long bits;
>     } SBlockInfo, *SBlockInfoPtr;
>
> SBlockInfoPtr block
> ((char *)(block->blockPtr)) += mChunkSize; // <--- Warning here
>

Hi Mark,

I didn't have my copy of Stroustrup here, but I did find this by
googling for "c++ lvalue cast":
http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=/com.ibm.vacpp6m.doc/language/ref/clrc05cplr188.htm

The interesting bit is:
"The cast operator is used for explicit type conversions. This operator
has the following form, where T is a type, and expr is an expression:

( T ) expr

It converts the value of expr to the type T. In C, the result of this
operation is not an lvalue. In C++, the result of this operation is an
lvalue if T is a reference; in all other cases, the result is an rvalue."

So your cast does not create a reference. This works without a warning:
        ((char *&)((*block).blockPtr)) += mChunkSize;

(Casts to a reference to a char *)

The simpler expression that also works without warning is:
        block->blockPtr = (char *)(block->blockPtr) + mChunkSize;

which avoids any attempt to cast the lvalue.


Thanks. That fixes the problem, and explains *why* it's a problem.

--
Mark
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Pointer not really an lvalue? (From: "Mark Wagner" <email@hidden>)
 >Re: Pointer not really an lvalue? (From: Rush Manbert <email@hidden>)

  • Prev by Date: Re: Pointer not really an lvalue?
  • Next by Date: Syntax highlighting for my own types
  • Previous by thread: Re: Pointer not really an lvalue?
  • Next by thread: Re: Pointer not really an lvalue?
  • Index(es):
    • Date
    • Thread