Pointers and Arrays in C

A free video tutorial from Tim Buchalka's Learn Programming Academy
Professional Programmers and Teachers - 2M students
Rating: 4.5 out of 5Instructor rating
59 courses
2,021,628 students
Pointers and Arrays

Learn more from the full course

C Programming For Beginners - Master the C Language

C Programming will increase career options. Become a better dev in other languages by learning C. Pointers explained

25:36:08 of on-demand video • Updated November 2023

Understand the fundamentals of the C Programming Language
Make yourself more marketable for entry level programming positions
Create your first C Application
Learn one of the most popular, widly used languages in the world
Understand variables and the different data types
Apply for real-time programming positions
Understand the core language that most modern languages are based on
Learn how to write high-quality code
Hello, and welcome back. In this lecture, I want to talk about the relationship between pointers and arrays. There is a strong relationship, and we need to understand this relationship before we can actually talk about pointer arithmetic. So let's get started. An array is a collection of objects of the same type that you can refer to using a single name. We know that we're pretty good at arrays by now. We've been using the character array for a few sections now. It's an array you can access by using an index or subscript inside those brackets. We remember that. We know that arrays are zero based they start at zero. But again,it's just a way to hold multiple months with the same variable name. A pointer is a variable that has its value, a memory address as its value, and it can reference another variable or constant of a given type. We also should at this point be pretty clear on what a pointer is. It just holds a memory address. You can use a pointer to hold the address of different variables at different times. But you have to have the same type. An int pointer has to point to restore the address of an int type. Arrays and pointers seem quite different, but they are very closely related and can sometimes be used interchangeably, especially with the character array. You can actually create a character a remember which is a string and you can create a character pointer which are essentially the same thing. There's only slight differences whether or not you allocate memory and free it with a character pointer. With a character array, you don't have to do that, but a character a is also fixed size. So if you create a character array string with 20 as its size, you can only store 19 characters. With the character pointer, you can use dynamic memory management and you can have the size increase or decrease, it can be more efficient concerning memory. But one of the most common uses of pointers in c is as pointers to arrays. This is when you most often use pointers, especially to characters. Character pointer is the same as a character array. The main reasons for using pointers to erase are ones of notational convenience and of program efficiency. So you can use either one an array or a pointer in many cases except when you're talking about dynamic memory management. But in most cases you can. And really the only difference is it's syntax, it's the notation. It's whether or not you use a subscript to access an element or whether or not you use point arithmetic whether or not you use the character pointer to point to the specific address of that element. It's also more efficient to use character pointers as well. Pointers to arrays generally result in code that uses less memory and executes faster. So you want to use a pointer for characters as much as possible and a pointer to other data types as well. Because it is faster and less memory use. Pointers over arrays for the most part. If you have an array of 100 integers, we know what this means, you just say int value is 100. 100 is the size. That means you can store 100 integers in that array. You can also define a pointer. We're going to name it values pointer since it's pointing to the values array and it can be used to access the integers contained in this array. So we just say instar values pointer. We know how to declare a pointer at this point. When you define a pointer that is used to point the elements of array, you do not designate the pointer as type pointer to array. Up until this point, all we've ever heard about is pointer to end, pointer to double. But when you're using arrays you don't say pointed array. That actual pointer is pointing to a specific element contained in the array. So keep that in mind. It's not actually entire array when you clear a pointer with the address of values. So to set the values pointer to point to the first element the values array, all you have to do is say values point or equals values. You don't have to put the address there or anything. Because values array is a pointer. That's what you have to drive in your head is all these arrays are pointers underneath the hood. So if you want to assign the first element of an array to a pointer, all you have to do is specify the array on the right hand side of the equals operator and on the left hand side you just have the values pointer. This is going to point to the first address in the array or the first element. The address operator is not used, notice that. The c compiler treats the appearance of an array name without a subscript as a pointer to the array. So specifying values without a subscript has the effect of producing a pointer to the first element of values. And so what this means is you can now point to subsequent values in the array by just incrementing the pointer, to go to the next address. And that's what we're going to talk about in the next few lectures when we talk about point arithmetic. And it's really just a notation thing and it's also an efficiency thing. So an equivalent way of producing a pointer to the start of values is to also apply the address of operator to the first element of right. So you can do it two ways, either way is fine. But you could also do this. You could say values pointer address of value sub zero. That will set the values pointer to the first element in the array. So you can use this one or you can just say values point equals value. Obviously, without the ampersand and without the subscript, it's a little more less code or easier to write. This may be a little more clear because you don't necessarily know that values is an array unless you look at the declaration. But again, either way is fine. Values pointer equals values or values pointer equals address of value sub zero. It's a matter of programmer preference. And again, it's a notational thing. So just to reiterate, when you have two expressions ar sub-i and asterisk ar plus i, those are equivalent in meaning. So we just want to make sure that you understand this. Both work if ar is the name of an array. You can use the pointer and you can de-reference it. And both work if it is a pointer variable. Use an expression such as ar plus plus only works if ar is a pointer variable. So you can't increment an array by saying plus plus and expect it to go to the next element. You can only do that on array pointers or pointers. So if we have ar that's a pointer pointing to array and we want to go to the next element we can just increment it every time. But if you say ar sub-i because ar sub i is an array and an array is a pointer, you can dereference it by saying ar which is ar is a pointer we've assigned ar plus i, it's the same thing. So in the next few lectures, we're going to talk about point arithmetic and this strongly relates to the relationship between arrays and pointers. Pointers can be arrays. It's not a pointer array, it's an element. So you can use the address of a pointer to navigate through an array, iterate through an array. Thank you.