Transcript for:
Introduction to Pointers in C

so in the series of learning programming in c we have discussed strings in C from this lecture I'm going to start pointers in C right first of all in this lecture we'll discuss some introduction about pointers like what is pointer need of pointer how to declare a pointer how to initialize a pointer with a simple example nothing much right after that one by one we'll discuss some more detail you know about pointers with some programs and all we'll also see some complicated programs on pointers right so first of all let me clear this thing pointers are it's not so tough for some students they think that pointers are like you know aliens for them it's very tough concept and see no it's not like that these are very simple pointer concept is very simple you just have to concentrate and you just have to you know try to understand with full concentration once you get once you you know get the basics of pointer like what is pointer right just how to declare and how to initialize and how to use a pointer that's it there is nothing much in pointers it's very simple to use pointers know concept right what is pointer before uh you know discussing pointer just I just want to recap that thing what is a variable how to declare a variable we have some fundamental lay types in see like int flow double care like if suppose I want to declare a variable of type int this is a variable name that's it and if this is the case declaration means space would be allocated to this variable variable name is a right and how many bytes four bytes integer will take four bytes like this is our computer memory it's a long Tye of bytes this is one b one b one b one b and somewhere four bytes like suppose these four bytes 1 2 3 4 these four bytes has been allocated to to this a having some address suppose th000 first bite address is th th000 then 1,001 2 and and three these four byes would be allocated to this a right so I'm not writing it something like this I'm just writing the four bits in a single block right I'm writing it it like this and address is what like obviously we will see we will suppose the address of a is what Base address Base address means the address of the first bite that would be address of this variable we consider it right so address would be 1,000 from 1,000 to 1,000 and three but we take Base address that is th000 only so for Simplicity purpose I'm writing this something like this I hope you got it right so now see this variable is having when suppose I'm initializing it now I'm going I haven't initialized it so it is having some garbage value so I'm initializing suppose it with 10 value so 10 would be stored here so this a is what name of the variable this is what value of this variable aable right and this is what address of the variable three things the variable will have right name value and address this is name of the variable this is what value and this is address of the variable right now pointer are also variables or we must say these are special variables these are not these kind of variables pointers are special variables which contain address of any other variable right pointer contains address of any other variable pointer does not contain any simple value like 10 five or like integer value or float value float if I take float variable like float B it is going to take floating type of know values like 1.1 right if I take care care C it will take character like a b c or if I write one in single code that is also considered as car if I write one this is considered as integer I hope you got it right if you declare a double type of variable that will contain you know double type of value but pointer always contain address address of any other variable right so now see how I hope you got what is a pointer or we can say po are derived data type this comes under the category of derived data type right because we drive it using these fundamentals data types right fine so now how to declare a pointer how to declare a pointer see this is this thing is very you know important please watch it carefully if I write something like this first we will write the general syntax would be data type typ then estri and then pointer name this is General synx to declare a variable right so if I write here int s and pointer name I'm taking suppose p and see what if you write this something like this like in estri this is also fine if you write int star p this is also fine in C if you write int star P these are same in C right so please don't get confused I'm writing something like this int Then star and then P so now if you write simply int P it means p is a variable which will contain which type of value integer type of value but if you write here this SRI it means p is what a pointer variable which will contain address of any other variable right and more specifically if I say address of a integer type of variable right here we cannot say like the data type of pointer is int no if I write something like this float estri P maybe you will say data type of this pointer is float here you will say data type of this pointer is int this is the you know mistake many students do but here here here also pointer will contain address here also pointer will contain address right in both the cases pointer will contain address so how you can say here dat type is in here type is float because pointer is containing address only right right so now what does this data type mean int float this if I write here like double double star P why I'm writing this in float double it means here this pointer p is containing address of a variable whose data type is integer right means here I can say p is containing address of a like right because see we can say a the data type of a is integer you can say this here P will contain address of a variable whose data type is float so P can contain here address of B Because data type of B is float here P will contain address of a variable whose data type is double I'm not writing here double if I declare double any know variable there you can say pointer will point you know address of that variable I hope you got like why this what does this data type mean right so the size of pointer is what 2 bytes for 32 bits machine right it depends right so now I hope you got the meaning of this thing and if you declare if you write so the basic you know what is pointer pointer will contain address of any other variable right so now here this is what declaration of pointer now here what can I say how to initialize a pointer how to initialize a pointer P equal to now P will contain address and how we will get address of any other variable using address of operator so address of what I can write here a address of a see here if you write p is equal to address of B it is not correct why because according to the Declaration of pointer P should contain address of a variable whose data type is iner but here I'm initializing p is equal to address of B I'm going to store address of B in P but data type of B is float so that would be illegal right this is not possible so now here you can only write down a name of the variable whose data type is integer so a is having in type so that is fine right so now see if you declare a pointer something like this then name obviously this is a variable this is special variable which would will contain address but this is also a variable it is going to take some space in memory right so and if in memory memory has been allocated to this P so obviously this will have its own address also right so address may be 2,000 see this address would be in hexadecimal form right but just for Simplicity purpose I'm taking these integers right so now I have initialized p is equal to address of a now address of a is 1,000 so in P we have 1,000 so now what we can say this p is pointing to a here we can write P points to a if somewhere somewhere it is written like any variable name PTR Point 2 a means definitely you will get PTR is a pointer which is containing address of variable a points two points 2 means it will it is containing address of this a right I hope you got the you know the Declaration and initialization of this pointer and what is a pointer right it's very simple you don't have to be very you know don't have to get confused in this case I hope you got meaning of this in Star P float starp and double starp right please it's not data type of pointer it is data type of that variable whose V whose address this pointer is going to store right and this declaration this is declaration of pointer this is initialization of pointer right so declaration is going to tell the compiler like three things this SRI means this would be a poter type of variable right and this p is having some space in memory some address in memory like 2000 maybe 2000 let us suppose 2000 third thing is this this is having address of a variable whose data type is integer so it is having address of integer data I right now uh you can do this declaration and this initialization in same step like I can declare something like this int star p and here only I can say is equal to address of a that is also fine in the same step or this is also fine you can merge this and this in one line also in a is equal to 10 in the same line I'm going to declare a pointer star p and I'm going to initialize it address of a this is also fine this will also you know uh uh points to this thing visualize you can visualize this thing something like this right but this is not correct if you just do something like this int star p is equal to address of a and after this I'm going to initialize this a a is equal to 10 this would be wrong why so because we are you are going to store address of a in this pointer but you haven't declared a yet you are declaring a after this so where is a here till here we don't have any a value or a variable name in memory right any address of a in memory so this would be illegal but yeah this is possible first declare a or you can say initialize this you can also initialize it or if you don't want to initialize then it will any garbage that is also fine like int a Comm star p is equal to add that is also fine right a will contain any garbage value but this is not you know uh this would be illegal this would be wrong fine so now see um now we have two way to print the value of a you can directly print print a percentage d and a and you can also print the value of a using this pointer because this pointer is also pointing to here but how you will print the value how you will access this value using pointer there we have to use some special operator that is D referencing operator or IND Direction operator this star that we'll discuss in next video right with some proper example and I'll show you that program on my laptop also here I just I just want to you know get you familiar with the with the basics of pointer right now one question for you is if I write something like this is this correct you have to tell me in comment box right two variable of type float X and Y one variable int a a pointer and in pointer I'm storing address of X and then address of a I'm storing right so which would be correct and which maybe which would be wrong and if both are correct then at last in pointer what would be the value you can take any you know address obviously you will take up to you 1,00 2,000 anything right and then you can tell me in comment box is it correct or not right so I hope this the basics of pointer you got and see if I write something like this end estri PTR so means here we have pointer name this is a pointer because of the sric we came to know name is PTR suppose address is 3,000 and here we haven't initialized this pointer so it is going to point some unknown location we don't know so the these you know un initialized pointers are very risky to use so before using the pointer we have to initialize these pointers in our program right and if you initialize like uh in Star PTR and if you initialize like like PTR is equal to address of B maybe compiler will not detect will not give any error because of this but it will give you wrong output so you have to take care while initializing pointer you have to take care of the data types because see B data type is float right and here it should point to integer data type if you write here a that would be fine right if I write here float B this sorry float star PTR then it is fine it will point to a variable whose data type is float and B is having flow data type so it will contain address of that is fine right so please you have to take care about these assignments you have to take care of the data type also fine so now that's it about the introduction of pointer in the next video we'll see some example and some you know that D referencing operator and some programs on pointers so I'll see in the next video till then bye-bye take yeah