From the course: Master C Language Pointers
Comparing arrays and pointers - C Tutorial
From the course: Master C Language Pointers
Comparing arrays and pointers
- [Instructor] An array is a buffer, a storage space for specific variable types. Its size isn't changed after its creation, and it uses unique notation in your C code. In this exercise file, you see an array's elements accessed and output individually. Now this construction should be familiar to you and friendly in this presentation. In this variation on the first exercise file, the address of each element is output. The address placeholder percent P is used in the printf statement at line nine and the ampersand address of operator is used on the element. Build and run. Now the specific addresses you see are different though each is separated by the size of an integer here, four bytes. So how would you perform the same feat by using a pointer? First it helps to understand the weird relationship between pointers and arrays. This code is based on the previous exercise file. Pointer variable P is added at line six. To set the pointer equal to the base of the array I will add after line seven, P equals a. That's it. No ampersand is required because A is an array. And I'll replace the print f statements argument here with variable p. No ampersand, because p contains an address. Save this change, build and run. And unfortunately, you see all the same addresses in the output. Now, this is different from the earlier example which showed the address of each element remember, which was separated by four bytes. To reference each element pointer p must be incremented, as shown in this modification, specifically within the for loop here at line 12. Because P is an integer pointer and referring back to line six here. Incrementing the pointer references the address of the next element in the array. Build and run. And these values correlate to each of the five elements and their addresses, separated by four bytes, the size of an integer. To access the values within the array, you need the asterisk operator, as shown in this exercise file in the printf statement at line 11. Build and run. And the output shows the values of each element in the array. You can also keep the address of pointer p the same and use variable X to access elements within the array as shown here in the printf statement modification line 11. Within the parentheses, the address stored at pointer P is increased by the value of variable x. Now this calculation doesn't mean that the value one or whatever is added to the address. Know the value of pointer P is increased by x integer sized chunks. The result is that the array elements are accessed and the asterisk operator outputs their values. And there you see the values. This slide shows the relationship between pointer notation and array notation. Further documentation is provided in a bonus exercise file, 02 01 PointerBonus1.c.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Comparing arrays and pointers3m 36s
-
(Locked)
Allocating storage3m 29s
-
(Locked)
Exploring allocation functions4m 58s
-
(Locked)
Accessing the buffer3m 41s
-
(Locked)
Working with pointers in structures2m 18s
-
(Locked)
Creating structure pointers3m 29s
-
(Locked)
Saving pointers and buffers4m 31s
-
(Locked)
Challenge: Saving and reading a record1m 6s
-
(Locked)
Solution: Saving and reading a record2m 15s
-
-
-