1#include <iostream>
2using namespace std;
3int main(){
4 //Pointer declaration
5 int *p, var=101;
6
7 //Assignment
8 p = &var;
9
10 cout<<"Address of var: "<<&var<<endl;
11 cout<<"Address of var: "<<p<<endl;
12 cout<<"Address of p: "<<&p<<endl;
13 cout<<"Value of var: "<<*p;
14 return 0;
15}
1#include <iostream>
2
3using namespace std;
4
5int main () {
6 int var = 20; // actual variable declaration.
7 int *ip; // pointer variable
8
9 ip = &var; // store address of var in pointer variable
10
11 cout << "Value of var variable: ";
12 cout << var << endl; //Prints "20"
13
14 // print the address stored in ip pointer variable
15 cout << "Address stored in ip variable: ";
16 cout << ip << endl; //Prints "b7f8yufs78fds"
17
18 // access the value at the address available in pointer
19 cout << "Value of *ip variable: ";
20 cout << *ip << endl; //Prints "20"
21
22 return 0;
23}
1#include <iostream>
2
3using namespace std;
4// isualize this on http://pythontutor.com/cpp.html#mode=edit
5int main()
6{
7 double* account_pointer = new double;
8 *account_pointer = 1000;
9 cout << "Allocated one new variable containing " << *account_pointer
10 << endl;
11 cout << endl;
12
13 int n = 10;
14 double* account_array = new double[n];
15 for (int i = 0; i < n; i++)
16 {
17 account_array[i] = 1000 * i;
18 }
19 cout << "Allocated an array of size " << n << endl;
20 for (int i = 0; i < n; i++)
21 {
22 cout << i << ": " << account_array[i] << endl;
23 }
24 cout << endl;
25
26 // Doubling the array capacity
27 double* bigger_array = new double[2 * n];
28 for (int i = 0; i < n; i++)
29 {
30 bigger_array[i] = account_array[i];
31 }
32 delete[] account_array; // Deleting smaller array
33 account_array = bigger_array;
34 n = 2 * n;
35
36 cout << "Now there is room for an additional element:" << endl;
37 account_array[10] = 10000;
38 cout << 10 << ": " << account_array[10] << endl;
39
40 delete account_pointer;
41 delete[] account_array; // Deleting larger array
42
43 return 0;
44}
1// Variable is used to store value
2int a = 5;
3cout << a; //output is 5
4
5// Pointer is used to store address of variable
6int a = 5;
7int *ab;
8ab = &a; //& is used get address of the variable
9cout << ab; // Output is address of variable
1#include <iostream>
2using std::cout;
3
4int main() {
5 /*
6 Some things to keep in mind:
7 -you shouldn't circumvent the type system if you are creating raw ptrs
8 and don't need to "type pun" or cast (don't use void ptrs)
9 -ptr types only reference memory (which are integers), not actual data, thus
10 they should not be treated as data types
11 char* is just 1 byte of mem, int* is just 4 bytes of mem, etc
12 - '*' means that you are creating a pointer which "points" to the mem address
13 of a variable
14 - '&', in this case, means "get the mem address of this variable"
15 */
16
17 void* ptr; // a pointer that doesn't reference a certain size of memory
18 int* int_ptr; // a pointer that points to data with
19 // only 4 bytes of memory (on stack)
20
21 int a = 5; // allocates 4 bytes of mem and stores "5" there (as a primitive)
22 ptr = &a; // can only access the memory address of 'a' (not the data there)
23
24 int b = 45;
25 int_ptr = &b; // can access both memory address and data of 'b'
26
27 cout << ptr << "\n"; // prints mem address of 'a'
28 /*cout << *ptr << "\n"; <- this will error out; a void ptr cannot be
29 derefrenced */
30 cout << *(int*)ptr << "\n"; // type punning to get around void ptr (extra work)
31
32 cout << int_ptr << "\n"; // mem address of b
33 cout << *int_ptr << "\n"; // data stored at b
34
35 /* -- OUTPUTS -- */
36 /*
37 some memory address (arbitrary) which contains 05 00 00 00 as its data
38 5
39 some memory address (arbitrary) which contains 2D 00 00 00 as its data
40 45
41 */
42
43 return 0; // you only need this if "main" isnt the linker entry point
44 // you also don't care
45
46 // ur also probably wondering why I didn't using namespace std... cherno
47}
1int* pointVar, var;
2var = 5;
3
4// assign address of var to pointVar
5pointVar = &var;
6
7// access value pointed by pointVar
8cout << *pointVar << endl; // Output: 5
9
10In the above code, the address of var is assigned to the pointVar pointer.
11We have used the *pointVar to get the value stored in that address.