From the course: Complete Guide to C Programming Foundations

Understanding pointers

- [Instructor] To understand pointers, you must explore variables. This code reports various details about integer variable alpha. From just reading the code, you can obtain three information tidbits. It's data type, which is integer, it's name, which is alpha, and the value it's assigned, which is 27. The other details are obtained when the program runs, the number of bytes alpha occupies in memory obtained by the size of operator at the end of line nine, and the variables location in memory obtained by using the ampersand operator in line 10, along with a percent P placeholder to output its address. On this system, integer variable alpha occupies four bytes of storage. The variable sits at memory address while you see the address here. That's its location in memory on this system. These two items, a variables size and memory and its address are related to the concept of pointers in the C language. A pointer is a variable that holds a memory location. This definition avoids using the description, a pointer points at an address, which is true, but that's confusing. It's better to say that a pointer is a variable that holds a memory location. Which memory location does a pointer reference? A pointer holds the address of another variable or buffer in the code because the pointer itself is a variable, it can be modified, changing the address in some way. It can also change or manipulate the data at the address the pointer holds. Pointers are declared like any variable. They have a data type and a variable name. The pointer's name is prefixed by an asterisk when it's declared. This is the unary asterisk operator, not the multiplication operator. Like all variables, a pointer must be initialized before it's used. An uni initialized pointer leads to trouble. To initialize a pointer, it's assigned the address of another variable in the code, one that matches the pointer's data type, int for int, char for char and so on. The ampersand operator is used to fetch the variables address. You saw this operator used earlier in the exercise file. You may also recall it's used in the scanf function. Pointers can be assigned to the address of a buffer or any allocated chunk of memory. Once assigned an address, a pointer has two personalities, which is yet another source of confusion. Used by itself, the pointer variable represents an address location in memory. Used with the asterisk operator, the pointer represents the data stored at that location. This is known as de-referencing the pointer. Regardless of how it's used, the pointer variable is always declared by using the asterisk operator. Pointers present a major hurdle in learning C, and many programmers do avoid them, which is wrong. As with any programming concept, the best way to get a grip on pointers is to see sample code and to experiment on your own. And if you forget, just repeat that a pointer is a variable that holds a memory location.

Contents