Written by:
blocks in computer memory are labelled
if you declare var A, the computer will allocate memory for that variable based on its type.
For a var A, which is an integer, a computer may allocate 4 bytes based on its architecture
the achitecture of the computer determines how many bytes are given for an interger, a char or a float, or any data type supported in the language.
for a typical achitecture
int | 4 bytes |
float | 4 bytes |
char | 1 byte |
double | 8 bytes |
a computer will allocate memory blocks starting at 204 (just an example memory address) to variable A if you declare it in your program.so
the address given to the variable will be stored in a look-up table. (this is a kind of memo so that the compiler can know where each variable is kept at)
So it will always check this look-up table, to get the address of A, then go there to read its data
if you store data in var A, it has to look up its address in the look-up table, then write you data, here the number "5" into that address of variable A
if you increment A, with a++
it find address of A from the look up table
it then increments the value of the integer stored at A from 5 to 6
Pointers are just variables that store the address of other variables. (A pointer is also a variable like an integer so it exists in memory at some address)
we have another variable p (a pointer)
this has now stored the address of integer variable A
we can run some operator on pointer P (which has the address of A) and reach A
P is stored at address 64 in this example.
P is also just a variable at an address but it just happens to be a pointer
now we have changed P to point at the next variable in memory, here variable B
we usually declare a normal variable using its type like this integer A
to declare a pointer, we have to put an * before the name of the poitner variable we are making
* is like something at says -> pointing to
so here we are making a pointer that points to an integer ( int *)
P is now a pointer var that points to an int
it is a variable, that can store the address of an integer
to store the address of A into pointer P
we get it using the & character
now we have stored the address of A into the pointer P
here we have saved the value 5 to integer variable A
if we print pointer P
it prints the address of variable A (204 address location)
if we print &A, we still print the address of interger variable A
if we put * infront of a pointer -> like (* pointer ) we get the value at the address it is pointing to
we are derefrencing because we have stored an address at P and now we are getting the value at the address
(* P) can be read as value at address stored in P
or simply as value at p
we can even change the value stored at A using the same syntax
we using *p = 8;
we are changing the value stored at address in P to 8
so a is now going to be 8
p -> is the address
*p -> is the value at address
programmatically A and *P are now the same thing
if we print P without *, we will print the address it has stored in it (here the address of A)
if we print P with * like *P we will print the value stored at A
you declare at an integer with int a
you declare a pointer with int * p
you can declare a character var with char c;
you can declare a pointer to a character using char * Pc
so you can declare a pointer to any data type
here we show how to declare a double and also how to declare a pointer to a double
you can even point to a user defined structure. (we shall see this later)
the & before a variable name gives you its address in memory
if we have the address of A stored at P
we can read the value of A by derefrencing the pointer P
time to see it in a real c program
we can not print P yet as we have not yet stored anything in that pointer.
A pointer needs to point to some address or we should save 'null' into it so if we print here we get an error
this is the error for printing an empty pointer
now we store the address of A at pointer P
printing this returns the address of A
we can prove that is is a really the address of A by printing &A
& -> is like an operator on a variable that returns its address
this syntax should return the value stored at address P
remember we read that as -> value at P (value that pointer P is pointing to)
the value at P here is A and we can see its a random number in memory because we didnot initialize it
if we initialize A to 10;
now we can see that the value at address stored in pointer P (which is the value of A) is printed as 10
by printing the address of A using the & operator, we see that it is the same value stored in pointer P
read *p as value at address p <- it is used to dereference the pointer to get the value stored at the address the pointer is pointing to
we can change the value stored in variable A using its pointer P since pointer P contains the address of A
by assigning a new value to *p (value at address in P) we changed the value of A in memory to 12 (without touching the variable A in our code 😁)
This is the most beautiful thing about the c language.
now we test to see if the value of a really changed by printing out A
the value of A is now 12
we make a new variable B and assign 20 to it
we change the value at address stored in P (which is A) to be the same value as b (20)
*p = b; is equivalent to a = b;
this means we are using the pointer to variable A to change its value to 20 (because B is 20)
now we print out the address stored in P (it is still the address of variable A)
however the value stored in A is now equal to 20
you can initialize the pointer variable in two lines
you can also just declare and assign a value to it as seen below
the space between the * and the variable name is not important
you can write this code as int* p or int *p <- it is all the same to the compiler
this is basically some operators you can do on a pointer variable.
if you increment a pointer to an integer (a pointer that points to an integer) by one, it is as if you are moving it from the current integer address to the next integer address.
It has to increase by the size of memory of the variable type it is pointing to
it will therefore move to point to the next integer in memory (this will be 4 block/ bytes away) because an integer is 4 bytes
int | 4 bytes |
float | 4 bytes |
char | 1 byte |
double | 8 bytes |
so p + 1 is not 2003, it is supposed to be 2002 + 4 = 2006
the slots 2002, 2003, 2004, 2005 are already accupied by the integer stored at 2002 address
because 2006 is the next integer is memory
p+1 increments p by 4 since the size on an integer variable is 4
if we print the size of an integer using the sizeOf operator from c
we will see that is 4 bytes
if we incrment it by 2, the value gets incremented by 8 bytes (2 integers for this compiler)
if it was pointer to char, it would get incremented by one bytes
if it was a pointer to a double it would incremented by 8 bytes
we can stil dereference or try to read the next integer stored in memory after P using the *(p + 1) syntax
we will get a gabbage value as that part of the memory was not initialized.
but as you can see once we have a pointer, we can jump around in memory and do whatever we want to do.
in you program you must be careful to always access the address you intended to, otherwise you might overwrite some other data in memory leading to unexecpted behaviours of your program
it is important to know that you can only run operations on pointers that result into a valid address.
You can add 1 to a pointer, to return a pointer address that points to the next address in memory of the same type (like the we saw above)
you can add 2 to to a pointer.
infact you can add any integer to it, that will point to another address.
you can subtract two pointers as this may lead to a valid address (as long as you dont get a negative result).
you cannot multiply since this may lead to an invalid address result
you cannot divide pointers as well.
int *ptr1, *ptr2, *ptr3;
ptr3 = ptr1 * ptr2; // Error: Multiplication of pointers
ptr3 = ptr1 / ptr2; // Error: Division of pointers
you can however do this operations, if you are derefencing and multiplying or dividing the values this pointers are pointing to
int a = 1, b = 2, c = 3;
int *ptr1 = &a;
int *ptr2 = &b;
int *ptr3 = &c;
*ptr3 = *ptr1 * *ptr2; // No error: c = a * b
*ptr3 = *ptr1 / *ptr2; // No error: c = a / b
pointers are strongly typed.
this means the type of variable, the pointer is pointing to, must be known by the compiler.
we need to know teh type of variable, so that we know the type of data we shall be derefrencing.
all data is stored as binary.
a compiler need to know waht type of data the binary at the memory so that is can process it correctly (as an integer, or. character)
thse are teh block of memory needed to store each of these data types
an integer a will occupy 4 bytes
it is stored as binary data
each 8 bits is one byte
byte 0 here is the least significant byte
we have assigned these random by continous addresses to these bytes
each address block is one byte
the last bit will store the sign of this integer, as a negative number or a positive number
we are can convert this binary number in memory back to base 10 and we will get 1025 like we started out with
we have a pointer P storing the address of A and thus pointing to A
it will store only the address of the first byte in memory as 200.
the only way to know the that this variable A occupies the blocks, 200, 201, 202 and 203 is to know that it is an integer variable that occupies 4 block/ bytes in memory
so during derefrencing
we know we have to look at the address 200 until 203 to read the value of this interger
the compiler needs to know the type of data it is dealing with
if P was. char pointer , pointing at address 200
the machine will only check one byte of data and read only the data at 200
this is because it knows that a character variable only occupies one block of memory
if it was a float, it would still be. 4 bytes, but the way the data is stored and handled by the machine is different from how it is stored and handled if it is a float or if it is an integer, even though both occupy 4 bytes in memory
we will print out hte size of an integer here
we will print the address of P and the value at P address -> the value of A which P points to
we will declare a pointer character now as p-zero
and store the address of integer A there to play a trick 😂
we will give the compiler the wrong data type of the address we are pointing to
here we get a compilation error as the compiler does not want to assign data of a pointer to an integer into a pointer to character
we will force P pointer to be character pointer by type-casting it to be a pointer to character using type casting syntax
we wil try to derenference the value that p-zero is pointing to
it is reading the value here as one.
during type casting, we store the address of the first byte into pointer p-zero
that is why this was printed as a one
we can try to add to a pointer P to see what address it changes to
* you can only add or subtract pointers (this is so that pointer arithmetic leads to a valid address in memory)
p+1 has returned a gabbage integer from memory
p-zero is still dereferencing to 1, the value of of the least signficant byte of A
p-zero + 1 as move moved to point to the next one-byte address which happens to be storing value 4 in binary
typecasting has some use cases that we will see later
we can have a general pointer that does not point to any data type
we call it a void pointer
we can assign it to another address of any kind, since it is a pointer to void
we dont need to do typecasting for this
since it is not refereced to any data type, we cannot deference it
we can see that address in void pointer is the same as the address of a
we can do arithmetic on it
it is not possible and we get an compilation error as teh pointer does not know by how many memory block to increment this pointer
we will use these later. lets jsut remember them
a poitner is jsut a variable to memory, so another pointer can point to it
assuming this is teh view of our computers memory
each block is one bytes
each block has an address
we assign some addresses here
if we have an integer variable as x and intialize it as x
some memory should be allocated for x
in a typical computer architecture , it will be 4 bytes as it is an integer
assuming address 225 is alocated to x integer
Last Updated:
Summarize & share videos seamlessly
Loading...