Todayโ€™s Outline


  • Recap: Variable and Memory
  • Pointer and its operations
  • Pass by Pointer
  • Array and Pointer

Recap: Variable and Memory


  • A variable is normally used to store data in the main memory
  • A variable has 5 attributes
    • Value
    • Type
    • Name
    • Address
    • Scope
  • The address of a variable is usually in hexadecimal
    • 0x00023AF0 for 32-bit computers
    • 0x00006AF8072CBEFF for 64-bit computers

Pointer and its operations


Definition of Pointer

  • A pointer is a variable that stores the memory address of another variable
  • When a pointer stores the address of a variable, we say the pointer is pointing to the variable
  • A pointer, like a normal variable, has a type. The pointer type is determined by the type of the variable it points to

Basic Pointer Operators: & and *

int x = 2;
int *xPtr = &x;        // * used as pointer type
cout << &x << endl;    // 0x16d29b2f8
cout << xPtr << endl;  // 0x16d29b2f8
cout << *xPtr << endl; // * used as dereference operator which prints 2
  • & address operator: get address of a variable
  • * is used in 2 ways
    • * in declaration: it indicates a pointer type
      • e.g. int *p is a pointer that points to an int variable
    • * in other statements: it is a dereference operator

Common Pointer Operations

p1 = &x;     // Set a pointer p1 point to a variable x
p2 = p1;     // p2 and p1 now points to the same memory area
*p2 = 10;    // Update the value of the variable pointed by a pointer
int x = *p2; // Retrieve the value of the variable pointed by a pointer

Common Errors

int x = 3;
char c = 'a';
char *ptr;
ptr = &x; // error: ptr can only point to char, not int
ptr = c;  // error: cannot assign a char to a pointer
          // A pointer can only store a memory address
ptr = &c;

Pass by Pointer


Pass-by-Reference vs Pass-by-Pointer

void myFunc(int &num) {
    num = 3;
}
 
int main() {
    int x = 2;
    myFunc(x);
    cout << x; // 3
    return 0;
}
void myFunc(int *intPtr) {
    *intPtr = 3;
}
 
int main() {
    int x = 2;
    myFunc(&x);
    cout << x; // 3
    return 0;
}

Pass-by-Pointer vs Pass-by-Reference

void doSth(char *a) {
    *a = 'a';
    *(++a) = 'b';
}
 
int main() {
    char str[] = "Hello";
    doSth(&str[1]);
    cout << str; // Hablo;
    return 0;
}
void doSth(char &a) {
    a = 'a';
    ++a = 'b';
}
 
int main() {
    char str[] = "Hello";
    doSth(str[1]);
    cout << str; // Hbllo;
    return 0;
}
void doSth(char &a) {
    a = 'a';
    char *p = &a;
    *(++p) = 'b';
}
 
int main() {
    char str[] = "Hello";
    doSth(str[1]);
    cout << str; // Hablo;
    return 0;
}

Example: Swapping Value

#include <iostream>
 
using namespace std;
 
void swap(int *p, int *q) {
    int tmp = *p;
    *p = *q;
    *q = tmp;
}
 
int main() {
    int a = 3, b = 7;
    swap(&a, &b);
    cout << a << " " << b << endl; // 7 3
    return 0;
}

Array and Pointer


char str[] = "Hello World";
char *p = &str[0];  // p is pointing 'H'
 
cout << *p << endl; // 'H'
cout << p << endl;  // 'Hello World'
 
p = &str[3];        // p is pointing 'l'
 
cout << *p << endl; // 'l'
cout << p << endl;  // 'lo World'
  • Supposed to print the address of 'H' but prints 'Hello World' because it is a string

Array Variable is NOT a Pointer

char str[6];
strcpy(str, "apple");
cout << str;
 
cout << sizeof(str) << endl; // 6, which is size of array
cout << sizeof(&str[0]);     // 4 or 8, address depends on computer
char str1[] = "Hello";
char str2[] = "World";
str1 = str2; // NOT allowed
 
char *ptr = str1;
cout << ptr << " ";
ptr = str2;
cout << ptr << endl;

Arrays as Parameters

void myFunc(char *myStr) {
    cout << sizeof(myStr); // 4 or 8, address depends on computer
}
 
void main() {
    char str[3];
    strcpy(str, "hi");
    cout << sizeof(str); // 3, which is size of array
    myFunc(str);
}