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.
Author
Released
5/1/2019- 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
Skill Level Intermediate
Duration
Views
Related Courses
-
Learning C
with Dan Gookin3h 16m Beginner -
Code Clinic: C
with Dan Gookin1h 7m Intermediate
-
Introduction
-
Using the exercise files1m 23s
-
Configuring the IDE2m 34s
-
1. Arrays and Structures
-
Understanding arrays2m 59s
-
Working with arrays3m 5s
-
Modifying arrays3m 11s
-
Building a structure3m 23s
-
Nesting structures2m 56s
-
Understanding a union2m 47s
-
-
2. Characters and Strings
-
Testing characters2m 34s
-
Fetching string input3m 18s
-
Sending string output2m 46s
-
Using printf() placeholders3m 36s
-
Exploring string functions3m 29s
-
Manipulating strings3m 53s
-
Avoiding string problems2m 27s
-
3. Pointers
-
Understanding pointers3m 19s
-
Doing pointer math2m 57s
-
Allocating storage3m 15s
-
Working with a pointer array3m 38s
-
Using pointers in structures3m 30s
-
Returning pointers2m 39s
-
-
4. Files and the Operating System
-
Reading from a file3m 25s
-
Writing to a file3m 24s
-
Working with raw data2m 53s
-
Using random file access2m 47s
-
Manipulating files2m 57s
-
Exploring the path2m 8s
-
Reading a directory2m 53s
-
Getting file information2m 46s
-
Using command line arguments2m 44s
-
Calling the operating system2m 46s
-
-
5. The Interesting and Extraordinary
-
Solving math puzzles3m 32s
-
Creating random numbers3m 42s
-
Working with time functions3m 54s
-
Sorting data4m 8s
-
Using a debugger4m 53s
-
Writing your own header file2m 53s
-
Challenge: Lotto simulation1m 18s
-
Solution: Lotto simulation2m 29s
-
-
Conclusion
-
Next steps1m 52s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Understanding pointers