• 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: Rush Manbert <email@hidden>
  • Date: Thu, 13 Jul 2006 11:55:33 -0700

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.

Here's the full contents of main.cpp from my C++ command line tool test project:

int main (int argc, char * const argv[]) {
    // insert code here...
	typedef unsigned int ASize;
	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;

	unsigned int mChunkSize = 1;

	SBlockInfoPtr block = new SBlockInfo;
	block->blockPtr = 0;

	std::cout << block->blockPtr << std::endl;

	char *bytePtr = (char *)(block->blockPtr);
	bytePtr += mChunkSize;
	block->blockPtr = bytePtr;
	std::cout << block->blockPtr << std::endl;

	block->blockPtr = (char *)(block->blockPtr) + mChunkSize;
	std::cout << block->blockPtr << std::endl;

	((char *)(block->blockPtr)) += mChunkSize;
	std::cout << block->blockPtr << std::endl;

	((char *&)((*block).blockPtr)) += mChunkSize;
	std::cout << block->blockPtr << std::endl;

	std::cout << "Hello, World!\n";
    return 0;
}

- Rush
_______________________________________________
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


  • Follow-Ups:
    • Re: Pointer not really an lvalue?
      • From: "Mark Wagner" <email@hidden>
References: 
 >Pointer not really an lvalue? (From: "Mark Wagner" <email@hidden>)

  • Prev by Date: Install_name_tool help
  • Next by Date: Re: Pointer not really an lvalue?
  • Previous by thread: Pointer not really an lvalue?
  • Next by thread: Re: Pointer not really an lvalue?
  • Index(es):
    • Date
    • Thread