Basic C programming 8: Address and Pointer¶

Yuto Omae @ Nihon University, Japan¶

  • We use online c compiler.
  • https://www.onlinegdb.com/online_c_compiler
  • https://www.programiz.com/c-programming/online-compiler/ (If onlinegdb do not work)
  • Pointer is one of the important features of C programming.
  • Because Pointer is a little bit difficult, I recommend learning pointer after becoming familiar with the C programming.

Code 1: Address¶

  • All variables have Address which is the location of memory in computer.
  • Here is the code for checking Address.
#include <stdio.h>
int main() {
    int  a[3]={1, 2, 3};
    int *pointer;
    pointer = a; // pointer is address of a[0]
    printf("Address of a[0]: %p", pointer);
}

Key point¶

  • int *pointer is called the pointer variable, and address of variables can be assigned to this.
  • By writing pointer = a, the address of a[0] can be assigning to pointer.
  • The value of pointer can be viewed by %p in the printf function.
  • When displaying 0x7ffdfa0e6c7c, this is the address of a[0].
  • Because the memory location depends on your environment, other locatins are probably displayed.

Code 2: Increment pointer¶

  • A pointer variable can be increased.
#include <stdio.h>
int main() {
    int  a[3]={1, 2, 3};
    int *pointer;
    pointer = a; // pointer is address of a[0]
    printf("Address of a[0]: %p \n", pointer);

    pointer = pointer + 1; // p is address of a[1]
    printf("Address of a[1]: %p", pointer);
}

Key point¶

  • pointer = pointer + 1 represents increment of the pointer variable pointer.
  • Because pointer is the addoress of a[0], pointer = pointer + 1 is the address of a[1].

Works¶

  • Check the address of a[2] by using increment pointer pointer = pointer + 1.

Code 3: Access original variable via pointer¶

  • We can access a original variable via a pointer variable.
#include <stdio.h>
int main() {
    int  a[3]={1, 2, 3};
    int *pointer;

    pointer = a; // pointer is address of a[0]

    // [*pointer] and a[0] are same.
    printf("value of a[0]: %d \n", a[0]); // a[0]
    printf("value of a[0]: %d \n", *pointer); // *pointer is a[0]

}

Key point¶

  • By adding * symbol to a pointer variable, e.g., *pointer, we can access the original variable a[0].
  • Because pointer is the address of a[0], *pointer is same as a[0].
  • By the above code, check that *pointer and a[0] is same.

Works¶

  • Check a[1] and a[2] via pointer variable (use increment pointer pointer = pointer + 1).

Code 4: Assigning new value to original value via pointer¶

  • A original value can be changed via a pointer variable.
#include <stdio.h>
int main() {
    int  a[3]={1, 2, 3};
    int *pointer;
    pointer = a;

    // before changing value
    printf("Value of a[0]: %d \n", a[0]);

    // after changing value
    *pointer = 30;
    printf("Value of a[0]: %d", a[0]);
}

Key point¶

  • Because pointer is the address of a[0], *pointer = 30 and a[0] = 30 are same meaning.

Works¶

  • Change values of a[1] and a[2] by using pointer = pointer + 1 and *pointer = X. Not use direct assignment, e.g., a[1] = 10.

Code 5: Change all values of array by for loop via pointer¶

  • The all elements of array-variable can be assigned by using for loop statement and pointer.
#include <stdio.h>
#define MAX_ARR 3
int main() {
    int  a[3]={1, 2, 3};
    int *pointer;
    pointer = a; // pointer is address of a[0]

    // change value a[0], a[1], a[2] via pointer
    for(int i = 0; i < MAX_ARR; i++){
        *pointer = (i+1)*10;
        pointer = pointer + 1;
    }

    // view
    for(int i = 0; i < MAX_ARR; i++){
        printf("a[%d] = %d \n", i, a[i]);
    }
}

Key point¶

We can assign valuest to a[0], a[1] and a[2] by using pointer = pointer + 1 and for` loop statement.

  • Because the code is for(int i = 0; i < MAX_ARR; i++),
  • When i = 0 then *pointer = (0+1)*10; -> a[0] = 10
  • When i = 1 then *pointer = (1+1)*10; -> a[1] = 20
  • When i = 2 then *pointer = (2+1)*10; -> a[2] = 30

Code 6: User-defined function for changing array values via pointer¶

  • We can make the user-defined function for changing arry-values via pointer.
#include <stdio.h>
#define MAX_ARR 3

// user-difined function for changing array val.
void change_arr_val(int *pointer){
    for(int i = 0; i < MAX_ARR; i++){
        *pointer = (i+1)*10;
        pointer = pointer + 1;
    }
}

int main() {
    int  a[MAX_ARR]={1, 2, 3};

    // change value a[0], a[1], a[2] via pointer
    change_arr_val(a);

    // view
    for(int i = 0; i < MAX_ARR; i++){
        printf("a[%d] = %d \n", i, a[i]);
    }
}

Key point¶

  • The input of the user-defined function change_arr_val is the pointer variable int *pointer.
  • We can change array-values in the change_arr_val by *pointer.
  • Because the function change_arr_val does not have return, it is the void-type function.

Assignments¶

  • (1) Make the user-defined function for assigning zero to all array-values by using pointer, e.g., when the array is a = {1, 2, 3}, output is a = {0, 0, 0}.