A pointer is a variable that holds a memory location, the address of another declared variable in a program. Pointers are declared as variables, but with the asterisk operator prefix. Pointers are used in two ways: without the asterisk, the pointer represents an address, with an asterisk, a pointer represents the value stored at its address. The ampersand operator is used to obtain the address of a variable. Always initialize pointers before you use them.
- [Instructor] To understand pointers, you must explore variables. In this code you see Integer variable alpha. Right away you know three things about it. Its data type, integer. Its name, alpha. And its value, 27, assigned at line 7. You can also obtain two other informative tidbits about variables in your code. The number of bytes it occupies in memory, obtained by the size of operator, which appears at the end of line 9. And the variable's location in memory which is obtained by using the & operator at the end of line 10. Build and run. And here you see that Integer variable alpha holds the value 27, that it occupies four bytes of storage on this system, and it sits at address 0060FEFC. And this address will be different from computer to computer but also on the same computer when you run the code at different times. It's not a guarantee. These two items, the variable size and its memory location, are related to the concept of pointers in the C language. A pointer is a variable that holds a memory location. That's my definition. It avoids using the description, a pointer points at an address, which is true, but it's better to say that a pointer is a variable that holds a memory location. A memory location of what? Well, a pointer holds a memory location or address of another variable in your code. Because a pointer itself is a variable, it can be modified, changing the address in some way. The pointer can also manipulate data stored at the address it references. Pointers are declared like any other variable. They have a data type and a variable name. The name is prefixed by an asterisk when the variable is declared. Now this is the single asterisk pointer operator. It's not the multiplication operator. Like all variables, a pointer must be initialized before it's used. An uninitialized pointer leads to trouble. To initialize a pointer, you assign it the address of another variable in your code, one that matches the pointer's data type. So int for integers, char for characters, and so on. The ampersand operator is used to fetch a variable's address. You may have seen this operator used in the scan F function, as well as in this movie's exercise file. Pointers can also be assigned to an allocated chunk of memory or buffer. Once assigned an address, a pointer has two personalities, which is yet another source of confusion. When used by itself, the pointer variable represents an address, location and memory of some other variable. When used with the asterisk operator, the pointer represents the data stored at that location. 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 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.
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions