Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1. Relationship between pointer and array
- 2. Pointer as function parameters in program
- 3. Pointer to structure
- */
- #include <iostream>
- using namespace std;
- void display(int *);
- struct robotStruct
- {
- string model;
- int version;
- };
- int main()
- {
- // 1. Relationship between pointer and array
- int array[5] = {1,2,3,4,5};
- cout << "array: " << array << endl; // array without index is address, 0x0001
- cout << "array[0]: " << array[0] << endl; // array with index is data, 1
- cout << "array + 1: " << (array+1) << endl << endl;
- int *ptr_array = array; // same since array already in address form
- // array access
- cout << "array[0]: " << array[0] << endl; // index array is data, 1
- cout << "ptr_array[0]: " << ptr_array[0] << endl; // index pointer is data, 1
- cout << "(array+1): " << (array+1) << endl; // array++ without index is adding 1 to address, 0x0001+1 = 0x0002
- cout << "*(array+1): " << *(array+1) << endl << endl; // dereference array++ same array[0+1], 2
- // 2. function in pointer
- display(ptr_array);
- // 3. pointer to structure
- robotStruct robot0 = {"UAV",5};
- robotStruct *rPtr;
- rPtr = &robot0;
- cout << "rPtr.model: " << rPtr -> model << endl; // UAV
- cout << "(*rPtr).version: " << (*rPtr).version << endl; // 5
- }
- void display(int *ptrTemp)
- {
- cout << "ptrTemp: " << ptrTemp << endl; // 0x0002
- cout << "*ptrTemp: " << *ptrTemp << endl; // 1
- cout << "*(ptrTemp+1): " << *(ptrTemp+1) << endl << endl; // 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement