Pointer Introduction Tutorial (revision 120925-r02KI)

Return to Lab 02

  • Create cobapointer.c file with the content below. Replace [nama_anda] with your name (example: Ramot Stephanus) and [NPM_anda] with your student ID (example: 1202000818) without the square brackets. Replace TOKEN with your token.
    $ vi cobapointer.c

    // Saya, [nama_anda] dengan NPM [NPM_anda], 
    // mengetik sendiri program ini. Saya bisa mengetik, yey! \:D/
    #include <stdio.h>
    int jj, kk;
    int *ptr;
    int main(void)
        jj = 1;
        kk = 2;
        ptr = &kk;
        printf("jj has the value %d and is stored at %p\n", jj, (void *)&jj);
        printf("kk has the value %d and is stored at %p\n", kk, (void *)&kk);
        printf("ptr has the value %p and is stored at %p\n", ptr, (void *)&ptr);
        printf("The value of the integer pointed to by ptr is %d\n", *ptr);
        return 0;

  • Compile the program so that you will have cobapointer executable.
    $ gcc -o cobapointer cobapointer.c
  • Run cobapointer and record the output to cobapointer.txt, see the result afterwards.
    $ ./cobapointer > cobapointer.txt
    $ cat cobapointer.txt

    jj has the value 1 and is stored at 0x8049740
    kk has the value 2 and is stored at 0x8049748
    ptr has the value 0x8049748 and is stored at 0x8049744
    The value of the integer pointed to by ptr is 2
  • The content of the address (0xnnnnnnn) can be different based on the memory allocation to your program.
  • From this pointer tutorial, you are expected to understand that the content of pointer (ptr) variable is the address of the variable (kk). We can access the content of variable (kk) by accessing the pointer variable with * sign in front of it (*ptr).
  • This is the end of Pointer Tutorial.

Return to Lab 02
Kembali ke Wiki