A Basic Intro to Pointers in C

A Basic Intro to Pointers in C

A pointer in C is simply a type of data that we can use inside of programs. The way an integer data type will hold a number, a pointer data type holds a memory address. Now, this memory address has to be the memory address of another variable, the way you can just think of a number and assign it to int i, you can’t just guess a memory address, it has to be an actual meaningful memory address in your machine. Also, you wouldn’t know this because your machine assigns a memory address to a variable randomly.

Now, where pointers get tricky is when trying to access the value stored in the memory address of a variable using pointers(simply dereferencing). Assume you have an integer variable storing an integer value say int score = 85; then int *pScore = &score; is a pointer variable storing the memory address of the score variable. To dereference pScore, it will be to prefix an asterisk to the pointer variable when displaying on std output which will grab the value in the address instead of the address itself. So, printf(“%d”, *pScore);displays 85 while printf(“%p”, pScore); displays the memory address whatever that may be.

I hope this helped you understand pointers a little better.