POINTERS

ASSIGNMENT

the & operator is used to retrieve a memory address

dataType variableName = {arbitraryValue};
dataType *ptrName = &variableName;

 * the ptrName will receive the memory address of "variableName" along with the "variableName's" data

USAGE

#include <stdio.h>

int main() {
    int variableName = 2;
    int *ptrName = &variableName;                   // Declare a pointer to an integer and assign the address of 'variableName'

    printf("Memory Address: %p\n", (void*)ptrName); // Print the memory address stored in 'ptrName' (which currently holds the address of 'variableName')

    return 0;
}

 * the output is the memory address of 'variableName'
 * Standardization: The %p format specifier in printf is designed to work with void*. While some compilers might be lenient, the C standard recommends casting to void* for consistency and portability.

DEREFERENCING

this means accessing the value stored at the memory location pointed to by a pointer

USAGE

Last updated