The address-of operator is a unary operator which returns the address of its operand: int x = 42; // instantiate a variable of type int std::cout << "Value of x: " << x << std::endl; // e.g., 42 std::cout << "Address of x: " << &x << std::endl; int* p = &x; // store the address of x in a pointer variable std::cout << "Value of p: " << p << std::endl; // e.g., the address of x std::cout << "Address of p: " << &p << std::endl; std::cout << "Value referred to by p: " << *p << std::endl; // e.g., 42
#include<iostream> #include<random> int main() { std::default_random_engine generator; std::uniform_int_distribution<int> distribution (1,9); std::cout << "Array : "; int a[10]; int* p = a; do { std:: cout << (*p = distribution (generator)) << '+'; } while (++p != a + 10); int sum = 0; p = a; while (p != a + 10) sum += *p++; std::cout << "\b=" << sum << std::endl; }
There is no STD that starts with the letter R. Some STDs are herpes, gonorrhea, syphilis, HIV, and chlamydia.
Kindergarten
#include<iostream> int main() { // Allocate some memory for an integer with value 42. int* p=new int(42); // // do some stuff with integer... // // Release the memory. delete(p); // Nullify the pointer. p=NULL; } Note that nullifying the pointer is not necessary when the pointer would fall from scope anyway (as in the above example), but it's good practice nonetheless. Consider the following: #include<iostream> int main() { int* p=new int; std::cout<<"0x"<<std::hex<<p<<" = "<<std::dec<<*p<<std::endl; delete(p); std::cout<<"0x"<<std::hex<<p<<" = "<<std::dec<<*p<<std::endl; } When you run the above example, you will note that even though we didn't initialise or alter the integer, it still has a value (whatever happens to reside at the memory location at the time). So although the program runs, it may or may not produce the desired behaviour depending on the value of *p. In this case we don't actually care what the value is, so the program appears to run normally. But the program has undefined behaviour because the value of *p is not under our control once we delete the pointer. If we nullify the pointer immediately after deleting it, the program will crash when we try to print the value of *p (an access violation will occur). While that is clearly undesirable, at least we immediately know we have a problem, and can alter the code accordingly: int main() { int* p=new int; std::cout<<"0x"<<std::hex<<p<<" = "<<std::dec<<*p<<std::endl; delete(p); p=NULL; // p is still valid (with the value 0x00000000), but *p is not! std::cout<<"0x"<<std::hex<<p<<std::endl; }
template<typename T> std::vector<T>* create_new_vector (const size_t M) { std::vector<T>* p = nullptr; try { if (p = new std::vector<T>) { p->resize (M); p->shrink_to_fit (); } } catch (std::exception& e) { throw e; } return p; }
It's an std.
The std code starts with 022
The Kingdom of Plants starts with p.
Potatoes are veggies. Potato starts with p. :)
A pointer is a variable that can be used to store any memory address, including the null address (represented by the nullptr value). To access any non-null memory address via a pointer, simply dereference the pointer. template<typename T> void f (T* p) { if (p==nullptr) return; // sanity-check std::cout<<"1. The address of p: 0x" << std::hex << &p << std::endl; std::cout<<"2. The address pointed to by p: 0x" << std::hex << p << std::endl; std::cout<<"3. The value pointed to by p is: " << *p << std::endl; } In the above example, output 3 shows how to dereference a pointer. Note that this example will only compile if std::ostream::operator<< is overloaded to handle a type T. All primitive data types such as int and float are supported by default but all user-defined types require an explicit overload.
The horse gait that starts with a 'p' is called Piaffe.