I am learning for my CPP exam and one of the questions will be like this: "How to get variable address and are there any variables whose address cannot be obtained"?
So first one is easy, you just use "&" operator, but are there any variables (mind you that question only concerns variables!) whose address cannot be accessed with ampersand?
Any help would be appreciated
To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.
An address is stored in a compound type known as a pointer type.
So not to happen all these, two variables can't have same addresses not in c, anywhere, you can transfer a variable address to store in a variable, later you will learn more about this topic in pointers in c language, Hope you understood!!
The return statement should not return a pointer that has the address of a local variable ( sum ) because, as soon as the function exits, all local variables are destroyed and your pointer will be pointing to someplace in the memory that you no longer own.
are there any variables whose address cannot be obtained?
You cannot get addresses of member variables of structs that are bit-fields.
From the C++11 Standard:
The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields.
but are there any variables (mind you that question only concerns variables!) whose address cannot be accessed with ampersand?
I think the question in your content is different from the one from the title. I assume the one in your content is what you want.
There are variables whose address can't be obtained by ampersand, because you can overload that operator.
The code below, &a
won't give you the address of a
.
#include <iostream>
struct foo {
int operator &() {
return 900;
}
};
int main() {
foo a;
std::cout << (&a) << "\n";
}
NOTE: Such variable's address can be obtained by other methods. Basically the principle is erasing the type so that the overloaded operator has no effect. std::addressof
implemented this functionality.
So as an example. The smallest thing you can address in C++ is a byte, and so attempting to access for example any of the 1 bit uint8_t
's inside this bitField
is not legal.
#include <iostream>
#include <cstdint>
struct bitField {
uint8_t n0 : 1;
uint8_t n1 : 1;
uint8_t n2 : 1;
uint8_t n3 : 1;
uint8_t n4 : 1;
uint8_t n5 : 1;
uint8_t n6 : 1;
uint8_t n7 : 1;
};
int main() {
bitField example;
// Can address the whole struct
std::cout << &example << '\n'; // FINE, addresses a byte
// Can not address for example n4 directly
std::cout << &example.n4; // ERROR, Can not address a bit
// Printing it's value is fine though
std::cout << example.n4 << '\n'; // FINE, not getting address
return 0;
}
As TheDude mentioned in the comment section however, the STL has a class std::bitset<N>
which offers a workaround for this. It basically wraps an array of bools. Still, the end result is indexing bytes, not bits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With