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.
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Practice BCA MCQs
- Check Computer Science Books
- Check C Books