Ad Space — Top Banner

E2126

C++ Builder Programming Language

Severity: Minor

What Does This Error Mean?

E2126 means you applied the & (address-of) operator to something that does not have a memory address — such as a literal, a register variable, a function return value (rvalue), or an arithmetic expression. Only lvalues — named variables with a stable memory location — can have their address taken. The fix is to store the value in a named variable first and then take its address.

Affected Models

  • C++ Builder 10.x (Alexandria)
  • C++ Builder 11 (Sydney)
  • C++ Builder 12 (Athens)
  • RAD Studio 11 and 12

Common Causes

  • Applying & to an integer literal or string literal
  • Applying & to the return value of a function call (which is a temporary rvalue)
  • Applying & to a variable declared with the register storage class
  • Applying & to an expression like a + b or a > b

How to Fix It

  1. Store the value in a named local variable first, then take the address of the variable.

    A named variable lives at a specific memory address for its lifetime. This is always the correct fix when you need a pointer to a value that was previously a temporary.

  2. If you are passing the address to a Windows API or VCL function that expects a pointer, make sure you are passing a pointer to a variable, not to a literal or expression.

    Windows API functions often take pointers to output parameters (like DWORD*). Declare the variable, call the API with its address, then read the result from the variable.

  3. Remove the register keyword if it appears on the variable whose address you need. The register keyword (deprecated in C++17) hints that the variable should be kept in a CPU register, which has no address.

    In modern C++ compilers, the register keyword is ignored and variables always have an address. Removing it is clean and correct.

Frequently Asked Questions

Why can I take the address of a string literal in C but not in C++?

In C, string literals are lvalues (they have fixed addresses in the data segment). C++ is stricter about rvalue semantics in some contexts. If you need a pointer to a string literal, declare a const char* or const char[] variable and take its address.