hey yeah it's you bro hope you're doing well and in this video i'm going to explain the basics of pointers in c so sit back relax and enjoy the show if you wouldn't mind please like comment and subscribe one like equals one prayer for the youtube algorithm oh yeah we've finally made it to pointers we're only about 40 topics in a pointer is a variable like reference that holds a memory address as a value to another variable array etc some tasks are performed more easily with pointers and here's a comprehensive list of a few of
the benefits in this video we're going to more or less focus on building a solid foundation with pointers and understanding how they work so let's say we have a variable int age and i'll give this some value a variable has a value and an address and let's display the value and the address of this variable using two print statements so first i'm going to display the address of this variable address of age then i will use the p format specifier to display an address in hexadecimal and we are displaying the address of age and ampersand
is the address of operator address of age and i would also like to display the value of age value of age and this is an integer and we will display age so variables have a value and an address this is the address of this variable and the value at this address as you know there's tons of different things we can do with a value of a variable but there are things that we can do with an address as well so we can actually store this address within a separate variable a variable like reference and that
is called a pointer so to create a pointer we will make sure these are of the same data type as the variable we're pointing to age is an integer so we will declare this pointer of the integer data type and the next step to declare a pointer is that we will use an asterisk this is the in direction operator and a common naming convention for pointers is that you type lowercase p the name of the variable you're going to point to but make the first letter uppercase and i'm going to set this equal to the
address of age so the address of age and the value at this variable are the same and let's test that theory so this time i'm going to display the address of age and the value of ph and this will display an address so change the format specifier from d to p and i'm going to turn this line into a comment for now okay so these addresses should both be the same the address of age as well as the value stored within ph so our pointer has its own address but the value stored within it is
an address and we can access the value at this address by using the indirection operator so this time i'm going to print the value of age and the value at stored address and to dereference a pointer you will type the pointer name ph appreciated with the indirection operator as my own personal nickname for the indirection operator i call it the value at address operator that's not any official name that's just how i think about it so we're extracting a value at the given address within this pointer so if i display the value of age and
the value at the stored address using the indirection operator well both of these are going to be the same so we have 21 stored within our edge and after dereferencing this pointer we're extracting the value at this given address so you use the indirection operator when you declare a pointer as well as when you want to access a value at the stored address so with the data types of the pointer you'll want to make sure they're consistent c is a strongly typed language so if i change the data type of my pointer to char using
my compiler i'll receive a warning initialization of char from incompatible pointer type int now the actual data type of a pointer is the same they use eight bytes to store an address so i'm going to print the size of our variable as well as our pointer so i'll change this data type back to what it was originally and this time i'm going to print the size of our age variable as well as the size of our pointer size of age and we're displaying an integer and this will be in bytes size of age size of
pointer age and then i'm gonna display a new line okay so the size of our age variable is four bytes it's an integer integers use four bytes so even though we declared our pointer as an integer the actual size of our pointer is going to be eight bytes that's enough to store a hexadecimal address so just as good practice since c is a strongly typed language you'll want to be sure that the data type of your pointer is consistent with the variable that it's pointing to now here's one thing that we can do with pointers
we can pass a pointer as an argument to a function so outside of my main function i'm going to declare a function void let's say print age first we'll do this with passing and integer so int age and i will display the value of age you are age years old and i will display my age variable then at the end we will pass in our variable print edge and i will pass in age for now i'm just going to turn all of these into comments okay so you know this works you are 21 years old
you could also pass in a pointer too so i'm going to this time pass in pointer age and we need to change the parameter from an integer to a pointer so precede the parameter name with the indirection operator and i'll rename this parameter as ph in order to access the value of the address stored within my pointer i need to de-reference so i will use the indirection operator then type my pointer name ph and this will do the same thing as before so yeah those are pointers oh and before i forget you can declare and
initialize a pointer in two steps but it would be good practice if you're declaring a pointer to assign a value of null since we already declared this pointer we do not need to use this indirection operator again when assigning a value so p age equals the address of age so it's considered good practice to assign null if you're declaring a pointer and not yet assigning a value so yeah that's basically a pointer it's a variable like reference that has a memory address as value to another variable array etc some tasks are performed more easily with
pointers and to declare a pointer you use the indirection operator and if you need to access a value stored within a pointer you type the pointer name preceded with the indirection operator so yeah that is a quick intro to pointers if you found this video helpful please give this video a thumbs up leave a random comment down below and subscribe if you'd like to become a fellow bro