C++
3 Apr 2023

Pointer

Pointer

Cpp-Pointer

image

Keypoints

  • Basics
  • Define a pointer
  • How to use a pointer
  • Storage of pointer
  • Void pointer and wild pointer
  • Const in pointer
  • Pointer and array
  • Pointer and function
  • BubbleSort example

Basics

  • It’s used to get the storage indirectly. Pointer is to save address. Simply, pointer is an address.

Define a pointer

int a = 10;
int * p;
p = &a;         // & is to get the address. As 16 not decimal.

How to use a pointer

int a = 10;
int * p;
p = &a; 
*p = 1000; 
cout<< "a = "<< a << endl;          // cout 1000
cout<< "*p = "<< *p << endl;        // cout 1000
  • Using * to decode pointer: to get the data stored in the address which popinter refers to.

Storage of pointer

  • Taking 8 byte every pointer in 64 system; 4 byte in 32 system whatever the data type is.
int a = 10;
int * p = &a;
cout << "sizeof (int *) = " << sizeof(p) << endl;       // output is 8

Void pointer and wild pointer

  • Void pointer refers the 0 space. Widly used in pointer initialization.
  • Void pointer isn’t accessible.
  • Avoid wild pointer
int *p = Null;  // Void pointer
*p = 100;       // Output an error as 0~255 storage code is for system usage, can't be used for pointer.
int *x = (int *)0x1100; // Wild pointer: refers to a storage directly.

const in pointer

  • const pointer: pointer reference (address) isn’t adjustable while its refered value is.
  • pointer to const: pointer reference (address) is adjustable while its refered value isn’t adjustable.
  • const pointer to const: both not adjustable
int a = 10;
int b = 10;
const int *p = &a;  // pointer to const
*p = 20;            // ERROR
p = &b;             // reachable
int a = 10;
int b = 10;
int * const p = &a; // const pointer
*p = 20;            // reachable
p = &b;             // ERROR
int a = 10;
int b = 10;
const int * const p = &a; // const pointer to const
*p = 20;            // ERROR
p = &b;             // ERROR

Pointer and array

  • const pointer: pointer reference (address) isn’t adjustable while its refered value is.
int arr[5] = {1,2,3,4,5};
int *p = arr;       // arr is the 1st address of array
cout << *p << endl; // output 1
p++;                // add one int byte (8 in 64 system/ 4 in 32 sysem) to pointer 
cout << *p << endl; // output 2; since arr is also int type, p pointer now refers to the 2nd address

int *p2 = arr;
for (int i = 0; i<5; i++){
    cout<< *p2 << endl;
    p2++;
}

Pointer and function

void swap(int *p1, int *p2){
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

int main(){
int a = 10;
int b = 20;
swap(&a, &b);   // value pass isn't OK while address is OK; 
                // Different address of parameter and argument though they share the same value;
                // So, address pass is OK.
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}

BubbleSort example

# include <iostream>
using namespace std;

void bubblesort(int *arr, int len){         // arr is a pointer, *arr is to get the value
    for (int i = 0; i<len; i++){
        for (int j = 0; j<len-i-1; j++){
            if (arr[j] > arr[j+1]){
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

void printarray(int *arr, int len){
    for (int i = 0; i < len; i++){
        cout<< arr[i] << endl;
    }
}

int main(){
    int arr[5] = {13,18,5,46,21};
    int len = sizeof(arr) / sizeof(arr[0]);
    bubblesort(arr, len);                   // arr is the variable name, also the pointer refers to the address.
    printarray(arr, len);
    return 0;
}

Tags:
0 comments