Is the address of a register variable accessible in c?

Can i access the address of a register variable in C.

No, you can’t take the address of a variable declared register.

It tells the compiler to try to use a CPU register, instead of RAM, to store the variable. Registers are in the CPU and much faster to access than RAM. But it’s only a suggestion to the compiler, and it may not follow through.

Hope it helps, Best Luck :slight_smile:

From C99 standard (pdf), Section 6.7.1:

The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1). Thus, the only operator that can be applied to an array declared with storage-class specifier register is sizeof
1 Like

Taking address of Register is illegal both in C and C++ !! compiler will throw an error !

@bugkiller- see the rubenko comment for sources… he supported my answer with reliable sources !

2 Likes

You’re probably wrong, it won’t throw an error, it will just ignore the specifier if you try to extract its address. And C and C++ both handle this issue differently, so you can’t generalize for both.

@bugkiller - since you know c++ well. please refer to C++ Bruce Eckel Page number 149. line number 18 . Line there is “you cannot compute the address of register variable.” categorized under restriction to Register variable.

For C , Refere Dennis Ritchie page number 84 line number 13 . written there is “it is not possible to take the address of register variable, regardless of whether the variable is actually placed in register”

In short, i mean you are wrong… !! you didn’t do your homework :stuck_out_tongue:

2 Likes

@rubenko >> Why Bruce Eckel? Refer to Section 7.1.1 of the C++ standard pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
A register specifier has the same semantics as an auto specifier together with a hint to the implementation that the object so declared will be heavily used. [Note: the hint can be ignored and in most implementations it will be ignored if the address of the object is taken. —end note]

1 Like

address of register variable ‘regVar’ requested
This the error GCC has thrown me when I requested the error of a register class variable. Check the Code:
`#include <stdio.h>

int main(void){
register int regVar = 10;
int *ptr = &regVar;

printf(“%d”, *ptr);
return 0;
}`