Hello, and welcome back.
In this lecture, I want to talk about defining pointers, mainly the syntax
for declaring reporter and then using the address of operator
to assign a memory address to a pointer.
So up until this point, we should have it drilled on our head
that a pointer is nothing more than a variable
that points to a memory address or contains a memory address as its value.
So declaring a pointer it's not the same as creating a normal pointer.
So you can't do something like pointer pointer.
That's not the way to declare a pointer. The syntax is slightly different.
You use an asterisk. So it's not enough to say that a variable is a pointer.
You also have to specify the kind of variable to which the pointer points.
Different variable types take up different amounts of storage.
Some pointer operations require knowledge of that storage size.
So just like a regular variable when you specify a data type,
when you create a variable of a pointer data type,
you also have to specify that data type.
You can declare a pointer to a variable of type int by doing the following,
int star or asterisk p number.
That is going to tell you that that's a pointer.
The int looks the same, but after that is the asterisk.
The asterisk signifies that a pointer, and then you specify the type
of the pointer itself. This is an int pointer.
The type of the variable with the name p numbers int star.
It can store the address of any variable of type int.
A pointer stores addresses. So
if you're going to store the address of another variable,
that variable has to defined as an integer type.
So here PI is pointing to int
variable, char star pc,
that's pointing to a character variable,
float star pf star pg, those are pointers to float variables.
That's how you're defining them, types matter.
The space between the asterisk and the pointer name is optional.
You don't have to put a space there. You can have no space.
What ends up happening,
and we're going to talk about dereferencing a variable is you usually use a space in the declaration
and then when you're dereferencing you don't use it.
So all these declarations should contain a space by convention.
The value of a pointer is an address,
so it has to be represented internally as an unsigned image on most systems.
So it still has to store something. It just so happens that it stores a hexadecimal number.
And even though it's storing it as it looks like an integer,
don't think of it as an integer type.
The same things that you can do with integers, you can't necessarily do with pointers and vice versa.
So even though if you try to print it out, it's going to look like, a number a hexadecimal number,
it doesn't mean you can treat it like an integer.
You can multiply one integer by another, for example,
but you cannot multiply one pointer by another.
A pointer really is a new type, it's not an integer type.
The percent p represents the format specified for pointers.
I'm going to go through displaying the different data for a pointer and we'll see how that's used.
The previous declarations
creates the variable but there is no initialization. There's no assignment there.
You always want to assign
a value to a pointer it's very dangerous if you do not initialize that.
So you want to initialize a pointer when you declare it.
If you don't know what to initialize it to
if it's not another variable's address or you didn't
allocate memory dynamically for it, you can use a null pointer.
So you can initialize a pointer so that it does not point to anything.
You do this using null.
You say instar p number equals null for a pointer to an int.
Null is a constant that is defined by the standard library.
It's the equivalent of a zero for a pointer.
It means that it doesn't point to anything.
It's guaranteed not to point to any location in memory.
So this is good you want to do this because what it means is you're not going to make any
mistakes by accidentally doing a buffer overflow
or writing to memory that you're not supposed to write to.
By initializing it to this, it actually limits some mistakes that you can make.
We talked about null a bit previously. We talked about the null character.
That concept is different from the null pointer.
A null pointer means it's not pointing to anything,and it should be initialized with that.
You need to add an include directive for standard def.h if you want to use the null
definition here.
The address of operator. If you wanted to assign
your pointer to address,
you can use the address operator of an existing variable.
So if you want to initialize your variable with the add or sub variable you have
you have already declared you use the address of operator, the ampersand.
We've seen this when we've discussed the scanf function.
Here we're assigning an integer value variable 99.
We're creating a pointer of type int and we're assigning it the address of number.
We can do this because number is an int
and the pointer that we created is also pointing to an int.
So all we have to do after the equals operator is use the ampersand.
And so now p number is going to contain the address of number.
The initial value of p number is the address of the variable number.
The declaration of number must precede the declaration of the pointer
that stores its address
because you cannot assign to a pointer
something that doesn't have memory allocated.
If you try to assign to that pointer number, number doesn't exist unless you do it after.
In order to assign an address to a pointer,
that address has to have memory allocated for it.
Otherwise, the compiler is going to complain.
Be careful though because there is nothing special about the declaration of a pointer.
You can declare regular variables and pointers in the same statement.
So you could say double value, p value f num.
In this statement here,
this is the only the second variable p val is a pointer, the other two are just doubles.
That's a little bit more explicit, a little more obvious
in that declaration. But what if you say in star pq,
this implies that they're both pointers and they're not.
We only have one int pointer here that's p,
the variable q is just to type int.
It's a common mistake to think that both p and q are pointers.
So be careful when you're declaring a pointer on many variables on the same line.
It's also a good idea to use names beginning with p as pointer names.
This will tell you that, that variable is a pointer by just looking at it.
So usually you do all your declarations of variables at the top of a program,
usually inside the main function.
And when you see those variables
used later in the program, you can easily identify the different
data types or the different pointer types because they have p as a prefix.
So this lecture just went over
how you can define, declare a pointer and then assign
an address of an existing variable to a pointer.
Now there are cases when you don't want to necessarily assign
an existing variable a value of pointer, maybe you want to allocate new memory.
We're going to talk about those in the subsequent lecture. But right now, we're going to keep it simple.
We're going to focus on using pointers of type ints
and different easy types like doubles or floats.
And we're going to concentrate on
assigning existing variables, the address of and see how that works.
So let's move forward. Thank you.