// Some examples of using pointers
// Marc Chee (cs1511@cse.unsw.edu.au), October 2020

#include <stdio.h>

// note that even though these two functions have
// the same name, they're actually different
// because they have different input parameters
void incrementInt(int n);
void incrementPointer(int *n);

int main(void) {
    int i = 100;
    // create a pointer called ip that points at i
    // it stores the address of i
    int *ip = &i;
    printf("%p is the address of i, which has the value %d\n", ip, *ip);
    
    incrementInt(i);
    printf("i, %d, hasn't changed.\n", i);
    incrementPointer(ip);
    printf("i, %d, has now changed.\n", i);
    
    int numbers[10];
    
    // showing that arrays are actually pointers
    // to chunks of memory
    printf("%p\n", &numbers[0]);
    printf("%p\n", numbers);

}

// this function will have no effect!
void incrementInt(int n) { 
    n = n + 1; 
}
// this function will affect whatever n is pointing at
void incrementPointer(int *n) { 
    *n = *n + 1; 
}

Resource created Tuesday 01 September 2020, 09:30:27 PM, last modified Thursday 08 October 2020, 12:29:28 AM.

file: pointers.c


Back to top

COMP1511 20T3 (Programming Fundamentals) is powered by WebCMS3
CRICOS Provider No. 00098G