Pointer Variable

A pointer variable can be declare as follows:

int *ptr

A pointer will have size 8. Pointer variable can be used to hold lvalue

[!important]
When declaring the pointer like this, there is no guarantee that this pointer will points to nothing (null pointer) because it depends on the system.

Therefore, if we want it to point to nothing, we need to declare int *ptr = NULL;

To be able to hold the lvalue, we can do something like this.

#include <stdio.h>

int main() {
    int k = 5;
    int *ptr = &k;

    printf("k is %p, ptr is %p\n", &k, ptr);
}

As a result, ptr now will be point to lvalue of k.

k is 0x16b01a95c, ptr is 0x16b01a95c

However, the lvalue of ptr might be difference:

#include <stdio.h>

int main() {
    int k = 5;
    int *ptr = &k;

    printf("k is %p, ptr is %p\nlvalue ptr is: %p\n", &k, ptr, &ptr);
}
k is 0x16ef9695c, ptr is 0x16ef9695c
lvalue ptr is: 0x16ef96950

To get the actual value of what the pointer is pointing to, we can dereference it by using *ptr, for example:


int main() {
    int k = 5;
    int *ptr = &k;

    printf("value of ptr is %d\n", *ptr);
}
value of ptr is 5