Hello, and welcome back.
We are in a new section. This section is named pointers.
This is probably the most important section of the course.
It's also probably the most difficult section of the course. The
concept of pointers and c is a tricky, tricky concept.
But we're going to go through it really slow.
We're going to provide a lot of lectures and go step by step.
We're going to start at the highest level and go into more and more detail.
You have to understand pointers if you want to take full advantage
of the c language. So it's very important.
So let's start at the top and talk about this concept of indirection,
which is essentially the same concept as a pointer.
So pointers are very similar to the concept of indirection.
You employ indirection in your everyday life. So for example,
if I'm describing what indirection means,
it would be like if you had to buy something new, a new product at your company.
So for example, say you had to buy a new ink cartridge for your printer.
If you're working at a large company what you would probably do
is you would have to go through a certain department
to buy any new purchases. So you'd probably go to the purchasing department.
You would probably call somebody up in purchasing
and ask them to order the new cartridge for you.
It would be their job to then find a local store
that provides the actual item that you want to order.
And what this is this means is you're not going and ordering that item directly by yourself.
You're asking somebody to do it for you.
And then through their expertise and their
knowledge of who has that product in inventory,
they get it back to you.
So this is a level of indirection concerning you. You don't go ahead and just
go down to the store and buy it. You go to the purchasing department, the person's department
has the knowledge that it goes and does everything for you.
This is the same concept of a pointer in programming.
In programming languages, indirection is the ability to reference something using a name
or reference or container instead of the value itself.
We use a form of interaction by using variable names.
We do not use the memory address to access data.
We use a variable name.
The most common form of indirection is the act of manipulating a value
through its memory address.
So in addition to a really
small description of indirection using a variable name to represent a memory address,
you also manipulate a value through its memory address using the address itself.
the address itself.
So a pointer provides an indirect means of accessing the value of a particular data item.
It's a variable whose value is a memory address,
and this is vital to understand.
We have learned variables up to this point that contain certain types of data,
numbers, characters,
doubles, floats with rational numbers.
Now we're going to have a variable
that doesn't store any of this type of data.
The type of data stores is a memory address.
The memory address is going to be represented as an integer,
a hexadecimal number,
but it's still going to be a memory address.
So it's going to be the value
of an address and another location of memory if it's an existing variable
or a new location that you dynamically allocate.
So just as there are reasons why it makes sense to go through the purchase
department to order new cartridges,
you don't have to know which particular store the cartridges are being ordered from for example.
There are also good reasons why it makes sense to use pointers and c
To use these variables that represent an address, a memory address.
We talked about memory addresses in the very beginning of the course.
And how whenever you run a program and you create variables those are stored in memory.
And that memory has a particular address. You have to know the address to get that data back,
just like you need to know the address of a house if you're going driving somewhere
or an address of a location.
So using pointers in your program is one of the most powerful tools available in the c language.
That's why it's important to understand.
Any program in c that is substantive is going to use pointers.
So you have to understand this concept.
And for programmers out there that have programmed more modern languages like java or
or c sharp,
you don't necessarily have to worry about this because
all of the memory allocation and
there's no real concept of pointers.
Even though under the hood,
java uses pointers because they have copies of references.
It's the same kind of concept. But in c, you explicitly
have a variable that represents an address.
Pointers are also one of the most confusing concepts of the c language.
They tend to be abused
they tend to not be fully understood, especially by new programmers.
So it's important that you get this concept figured out at the beginning
and have a clear idea of what is happening
as you go into more and more advanced c and more and more advanced pointers concept.
So if you don't understand this in the beginning,
study, study study, practice writing code with pointers.
The compiler has to know the type of data stored
in the variable to which it points. So it's just like a regular variable
and that you have to specify the data type when you create the pointer.
The reason the compiler needs to know this is the same reason for variables,
it needs to know how much data to allocate even if you're pointing to some data.
Every pointer is going to be associated with a specific variable type.
It can be used only to point to variables of that type,
we'll give an example of this when we talk about defining a pointer.
So for example pointers of type
pointer to it can only point to variables of type int.
Again, a variable
has a value of an address, that's a pointer.
That address may contain a value somewhere in memory.
Pointers of type pointer to float can only point to variables of type float.
So here's an illustrative idea
that gives you the concept of a pointer.
Don't worry about the syntax here.
We've briefly discussed it in previous lectures this ampersand and this asterisk
you don't have to necessarily worry about that. What I want you to pay attention here
is how a pointer it has an address,
and that address can contain a value but be somewhere else in memory.
So in this example, we're creating a regular number,
we've created a million times, then you see this syntax with the asterisk
that signifies that it's a pointer.We're going to go through that in details.
You also see the ampersand, which signifies it's an address.
So in this example when we create this pointer of type int p number,
we're assigning an address. Because remember, pointers can only store addresses.
And how this is actually laid out in memory
is you have a variable name number,
and number has a value of 99.
Number also has an address in memory.
That address in memory is the value of the pointer.
So p number is a pointer that's pointing to address 1008.
1008 has a value of 99,
contains the the variable number as well.
So this is really what we're getting at. If this is not clear, you need to look at this for for
an hour and make sure you understand it.
Again, the point is a pointer has
a value to an address, that's what it stores.
And then you can actually access the value in that address using certain syntax.
So why use pointers, why even broach this concept,
why can't we just use existing
data types that we use and use the character array when we want to use strings.
Well, accessing data by means of only variables is very limiting.
With pointers, you can access any location, you can treat any position of memory
as a variable for example and then you can perform arithmetic
with pointers as well which makes it makes things easier.
Pointers in c make it easier to use arrays and strings,
and we're going to go through that as well, that's another advantage to use pointers.
Pointers allow you to refer to the same space and memory with multiple locations.
So what this means is you can update memory in one location
and then the change can be seen from another location in your program.
It's a way of sort of using global data but still having encapsulated inside functions.
You can also save a lot of memory space by being able to share components
in your data structures.
So remember, when c was invented, c was invented in the 70s.
The idea of c was to be very efficient and to be low level.
The idea of accessing memory directly via pointers
is very efficient,
and it's also very fast.
Pointers also allow functions to modify data passed
on them as variables. This is passed by reference. We're going to have a lecture on this.
This is where you can pass arguments to a function with your pointers.
And then inside the function, when you change that data,
the change will be reflected outside that function.
So we no longer have to use global data.
We can use data inside of a function,pass that data
as a pointer and then that data outside that function will be
allocated. So it's not having the disadvantage
of using global variables where everything is tightly coupled,
you can limit that to only the data that needs to only the functions that need to know that data.
As I mentioned, it can also be used to optimize the program to run faster
and use less memory than it would otherwise
which is an important concept,
especially in embedded real-time programs.
Pointers also allow us to get multiple values from a function.
We've talked about functions on how you can pass data functions.
We've also talked about how you can return data.
We know that right now you can only return one value from a function.
But if you use pointers, you can actually return multiple values from a function.
And we're going to talk about that. That's a big advantage.
We're also going to talk about dynamic memory allocation.
Right now when we create memory for a program,
it's done implicitly by the compiler when you declare a variable.
If you declare an int variable, it automatically creates 4 bytes for that variable for example.
When you're using arrays and you specify the size of an array,
that size is allocated at compile time.
So if you're trying to store more data than that, you're screwed,
your program's not going to work.
But if you had the ability to actually create
as much memory as you needed at runtime,
that would be huge. And pointers allow you to do this.
They allow you to create memory at runtime dynamic, memory allocation.
We're going to be talking all about this as well.
Pointers allow us to design and develop more complex data structures.
And data structure is something that stores data.
Examples of data structures are stack a cue or a linked list.
And we can create those much easier using pointers.
Pointers provide direct memory access, again, efficient fast.
So pointers are going to be a big concept. We're going to have a lot of lectures on pointers.
I want you guys to focus, and I want you to pay attention when i talk about all these concepts
because it's important and vital to understanding the c programming language.
So good luck, and let's get started. Thank you.