1#include <stdio.h>
2
3int main () {
4
5 int var = 20; /* actual variable declaration */
6 int *ip; /* pointer variable declaration */
7
8 ip = &var; /* store address of var in pointer variable*/
9
10 printf("Address of var variable: %x\n", &var );
11
12 /* address stored in pointer variable */
13 printf("Address stored in ip variable: %x\n", ip );
14
15 /* access the value using the pointer */
16 printf("Value of *ip variable: %d\n", *ip );
17
18 return 0;
19}
1/*
2**use of pointers is that here even if we change the value of c since the adderss is
3* of c is assigned to pc it *pc will also get modified*/
4int* pc, c;
5c = 5;
6pc = &c;
7*pc = 1;
8printf("%d", *pc); // Ouptut: 1
9printf("%d", c); // Output: 1
10
1#include <stdio.h>
2int main() {
3 int c = 5;
4 int *p = &c;
5
6 printf("%d", *p); // 5
7 return 0;
8}
1int* pc, c, d;
2c = 5;
3d = -15;
4
5pc = &c; printf("%d", *pc); // Output: 5
6pc = &d; printf("%d", *pc); // Ouptut: -15
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}