Understanding C Pointers
Understanding C Pointers for Programming is a simplified way to grasp the whole notion of the C programming language. As a Computer Engineering student myself, I have seen the many struggles of others while I earned A+ grades. I have done this ebook to help. View an easy to understand video on array and function in C Pointers explained in a simple way for struggling College students. More
C Pointers Basic Concept – Riko H
Welcome to the world of pointers! If you are reading this then that means you probably have some trouble with pointers or maybe just want some extra information on the topic. Let’s begin! If we are planning to dive into this vast world we first need to talk about a couple things. For starters we must understand what exactly a pointer is and how does it function in principle.
You might be tempted to or have already searched the internet high and low for a quick, simple and easy definition of a pointer and came up with something like: “a pointer is a programming language data type whose value refers directly to another value stored elsewhere in the computer memory using its address….”
It might then go into referencing registers and using all kinds of complicated analogies to explain the concept leaving you more confused than when you started reading.
Now, time for that simple explanation you were waiting for. The first question anyone may ask is:
“What is a pointer and how do they work?”
Well, a pointer in programming is simply a reference to an actual variable. How do they work? I shall explain the concept two ways:
First, let’s imagine we have a mailman, some mail and a house for the mail to be delivered. We can look at the data as the mail, the variable as the house and the pointer as the mailman. What does a mailman do? You guessed it! Deliver! If you have a package to be delivered (i.e. data to be stored) you give the address of the house to the mailman, then you give the mailman the package, however, you expect that if you access/look into the house you would see the package.
Similarly, when you use pointers to manipulate data you give data to the
pointer but when the variable it is referencing is accessed it too shall have the data because the pointer (just like the mailman) is the gateway to the variable.
If that concept didn't clear up anything for you let’s look at it another way:
Let’s say we have a funnel, a marble and a bottle. The bottle is our variable, the marble our data and the funnel as our pointer. If we were to insert the funnel to our bottle and put the marble in the funnel what would you expect to happen? The marble would fall in the bottle. What if you look through the funnel or look directly in the bottle, what would you see? The marble.
Similarly, when you use pointers to manipulate data you give data to the pointer but when the variable it is referencing is accessed it too shall have the data because the pointer (just like the funnel) is the gateway to the variable. If it sounds trivial or really simple that’s because it isn't complicated once you have the right imagination.
This brings up a really important point about pointers (no pun intended). As with the second analogy, you’ll notice that the data (marble) is actually in the variable (bottle) and not in the pointer (funnel) itself. That’s because pointers don’t actually store data, they just tell the data where to go.
That’s really it guys, that’s how simple the concept of a pointer is.
Available ebook formats: