 Hello friends, I am Sanjay Gupta. I recommend you on Sanjay Gupta Tech School. In this video, I am going to explain how you can count length of string with help of pointer. Before start, if you go to description of this video, you will find links of various playlists related to C language. You can follow them to watch videos. Now, first I am going to declare a string. Then I will tell you how we can process this with help of pointer. So let's say in the beginning, I have character array or string. Its size is 10 and here I am assigning India into it. So you can assign string within double quotes directly to a character array. So after this, 10 blocks will be allocated. These are the indexes. This is the name of this character array and it will be having these values assigned. After assigning India, it will be having null at the end. So these are indexes. In middle, we have all the values and at top I am writing addresses. So all these are addresses and remember that I have assumed these addresses. Because wherever you declare any variable in programming and you run that program on laptop. So your system automatically assigns a particular address or you can say the available address to that variable. So here we declared a variable named as str that is known as string as well. So this string is having 10 locations. So all these 10 locations will be having 10 different addresses. Because in C programming, character is having one byte of memory. Character variable allocates one byte of memory or you can say occupies one byte of memory. So here size is 10. So total 10 blocks are available. So they are having 10 different addresses one for each byte. If we have integer array then each block will be having 2 bytes or 2 addresses. And if we have float then each block will be having 4 bytes or 4 addresses. So this way it works. So now I am going to use this character array of string with help of pointer. So here I am going to declare a pointer of character. So remember that if you want to process character array or string then pointer must be of type character because whenever we are going to one location to another so we need to know how much bytes we need to skip or jump. So if pointer is of type character then it will always jump one byte. If it is of integer type so it will jump 2 bytes. If it is of float type then it will jump 4 bytes. So according to the variable you need to declare pointer of that type only. Now we need to count the length. So I am declaring one integer variable that will be initialized with C equals to 0. Now through this pointer I am going to access all the locations of this character array. So I am writing P equals to STR. So here you need to remember that whenever we use name of any array so it automatically provides base address of that location. So this is P and it will be having 101. So this 101 is also known as base address. And don't remember like 101. So whatever will be the address, beginning address will be known as base address. So right now it is 101 so 101 is base address. If it is 201 so then 201 will be the base address. So whatever will be the beginning address that will become base address and if you use name of any array whether it is of type character, integer, float they automatically provides beginning address to any pointer. So this pointer is having 101 as the beginning or base address. Now I can apply why you having this. STR is P not equals to null. So I am going to explain this first let me write on the statements. So C++ for counting purpose, P++ so that pointer can move to next location so that we can identify that locations value. And after completion of this loop I can write length equals to percent D and C. Now I am going to explain this loop so that you can understand how length will be identified with the help of pointer. So here you can see in this loop STR is not used anywhere in this loop. STR is not used anywhere. In place of STR I am using the pointer which will provide me all the available values so that I can count how many characters are there. So first understand the meaning of this asterisk. Its meaning is value at address. Asterisk means value at address. So right now P is having 101 so we can write it like this asterisk 101. So this asterisk P will convert into asterisk 101 and meaning of asterisk is value at address so value at 101 is I. So now check I not equals to null so it is true. So I hope you understood how through this pointer I am able to access the value. So pointer is having address of that location so instead of accessing this location through its name I am accessing its location through its address. So I is not equals to null so C will be incremented so initially C is 0 so now it will be 1 then P will be incremented. So I already explained you if we increase any pointer so its address will be incremented as per its data type. So P pointer is a character type so P pointer address that is available inside that pointer will be increased by 1. So it will be having 102. If you have pointer of integer type so integer pointer always increases the address which is stored inside that pointer by 2 and flowed by 4. So now P is having 102 as address so again dereference it so this time you can dereference it like this asterisk 102. So again value at address 102 it is N not equals to null so C will be incremented again P will be incremented to 103. So as per 103 value is D is not equals to null so C will be incremented to 3 again P will be incremented so it is 104. So check value at 104 it is I not equals to null 2 P will be 4 P will be incremented to 105. Check address 105 so 105 value is A which is again not equals to null so P will be incremented to 5 and P will be incremented to 106 and dereference 106. So value at 106 is null so this time loop will be false and after termination of loop this printer will be displaying length equals to personally C. So value of C is 5 so 5 will be printed here and you can count total number of characters are 5 so we are not counting null we are counting the characters which are available before none. So this way you saw how I iterate this loop with the help of pointer only without using the variable name. So whenever you want to access any array whether it is character type integer type or float type using its address. So you can use pointer assign base address or beginning address of that array to that pointer increase that pointer one by one and to address you can access a particular value and you can process whatever you want to perform. So I hope you understood how I explained you this process. So you understood how we can process string using pointer and in string we have null so you need to increase the pointer till null comes. If you are on the null position you can terminate the loop and you can print or you can show the outcome to the user. So I hope you understood whatever I explained related to string and pointer in this video. If you want to watch more videos open my channel and then you will find lots of video or you can go to description of this video where you will find various playlist related to C language. So you can watch them too. So thank you for watching this video.