Addressof Operator and Register Variable in C

Question: Can we use address of Operator (&) to Determine Address of a Register Variable in C

Answer: addressof operator ‘&’ is a unary operator fetches us address of its operand/data object occupied in memory. But registers are not the part of memory. Instead, these are components of CPU. Further, there are only a few registers that are used for data storage. Therefore, addressof operator doesn’t get us address of operands of type register. Well! Whenever we declare a variable to be register, this generates instructions suggesting compiler to allocate variable storage in the register memory but not a compulsion. Instead, this is fully dependent on compiler’s choice if to give operand storage in memory or registers. For example:

/*
 * addof_vs_reg.c -- program shows if we can fetch address of variable of
 * type register
 */
#include <stdio.h>
 
int main(void)
{
    int me = 1;
    register int you = 2;             /* 'you' declared to be register */
 
    printf("Address of me in exp. \"int me = 1\" is \t\t%p\nAddress of you "
           "in exp. \"register int you = 2\" is %p\n", &me, &you);
 
    return 0;
}

Observe the output below:

addof_vs_reg.c: In function ‘main’:
addof_vs_reg.c:9:3: error: address of register variable ‘you’ requested

Requesting for address of a variable declared to be type register causes compile time error with massage as “error: address of register variable ‘you’ requested”.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.