C_99 Returning Pointer from Function in C | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning c programming we are discussing functions in c we have discussed how to pass a string as an argument to a function how to return a string from a function in c in this video we will discuss how to return a pointer from a function say returning a string is also it's like returning a pointer character pointer because there also we were returning the address of the first character of the string that is nothing but obviously it's a pointer right but here you can also return an integer pointer float pointer from a function that we will discuss here you will take example of array and that thing will discuss here one by one with proper program and i will show you the program practically also we will run that program also right and also some important point about this concept also no we are going to discuss in this video and we can also pass a pointer as an argument to a function but that is nothing but call by reference method that we have already discussed call by value call by reference so here we will discuss how to return a pointer from a function but before that this will tell you one thing if you want to build a great career in software development then you must think about the aspects like what's trending in the industry what is the thought process behind the great application like like amazon flipkart zomato ola how to improve your code share ranking so here and again brings you a platform where you can get access to weekly shows which you can watch live and the host of these shows are working in some of the top companies like amazon google linkedin and our seven star coders on code chef and industry experts with years of experience so here they'll be covering the content which will be really helpful for your career like in the live episodes you can ask you can get opportunity to ask take a chat about the top 20 questions being asked and the industry leaders about the recruitment process you know in top startups and mncs and what is the eligibility criteria and how to apply for them even you can also get your resume reviewed by the exports sdgs not only this you can also participate into mock interviews and you can learn courses on programming languages cloud computing blockchain and crypto tech aspects of digital marketing and data analytics and have you ever thought about how these frontline apps like zometo paytm ola practo amazon flipkart were made and what was the thought process so here is a show to answer all these questions in this show anush is going to discuss the architectural breakdown of all the popular apps and learners will get to know how these apps were made and how these apps work so you can find out the detail of the show and the batch in the link that i'll put in the description box of this video and don't forget to use my code jkl10 to get 10 extra discount on your subscription so now let's discuss how to return a pointer from a function here i am going to take a simple example so if i am taking a function name suppose return pointer function name is return pointer only you can take any function name right and i'm going to take an array which we will pass here right and then in this function i'll increment the you know the address of that pointer that you are passing that address by two or four any number and then we are going to return that address right whatever i am saying that i will show you here what what i am saying what does that mean so this is function name and here we i want to pass an array integer type so just you have to write down in this this indication part that's it right no need to write down the name of the array now the main question is what would be the return type here we are returning a pointer so what would be the return type here i am taking integer type of array right so here pointer means int string if you simply write in it means it will return integer value but we are we don't you don't want the function will return integer value we want that this would return a pointer so in s3 integer pointer so it means this function is going to accept array as an argument and is going to return integer pointer or you can say pointer to integer that's it right now this is what the declaration in main what i'll call this function right so it is returning a pointer it means we have to accept that value whatever this function is returning and it is returning a pointer type of value it is a pointer so to return that thing also will take integer asterisk suppose i am taking p right and one more thing i am taking int array and i am initializing this one two three four five and i am calling this function return pointer how to pass error as an argument just pass the name of that that's it only one argument i want to pass right whatever it is going to return i'm going to store that in p p is what it is a type of pointer and it is also going to return integer pointer so obviously we have to accept that thing in the same time it is a pointer so after returning just printf percentage d and strict p whatever the value at this address that we are going to bring that's it now what would be the definition part of this function strict name of the function is return pointer and here i am taking an array to accept this thing right so this array is what it's called call by reference note call by value we have already discussed how to pass array as an argument so here we are passing the address of the first element so this is what it's not adding here we are not going to copy the complete one two three four five the values no here it will act compiler will interpret this line as something like this int asterisk a means here a is a pointer which will store the address of the first element whatever we are passing here right this thing we have already discussed in one of my previous videos you can watch that video first now here i am going to update this a is equal to a plus 2 and just return a that's it right now see whenever you will execute this what will happen first of all control will go here in the main function right if you will not write here anything it will consider that the compiler consider it as a no in so you have to write down a return statement so if you write down here void then means it it is not returning anything so first to the main now here we are taking what so what will happen generally the memory which would be allocated to this application this program will be divided into you can say four parts some memory to store the instruction whatever we are going to return here some memory for static and global variable one part of memory is stack memory and one is heap memory so whatever you are declaring the local variables and these that would be stored in the stack memory of this function stack memory of this program so the main function for memory has been allocated to this main function right so now once from that stack one stack frame would be for this main suppose this is for main so here we have a pointer p here we have what an array a so here i am just you know writing this one two three four five so here the address of this is what hundred then one zero four one zero eight one one two one one six i'm supposing integer is taking four bytes so in a we are having hundred so a is pointing to here a is acting as an internal pointer constant internal pointer to this array right so base address of this error is 100 that is that it will be a so return pointer here we are calling this function now control will go here now for this function also one stack frame would be a right here we have in a so this is nothing but a pointer right and here what what you are passing a we are passing here it means we are passing 100 so in this a also we are having 100 so this is also pointing to here now right a is equal to a plus 2 means we are incrementing the pointer it it doesn't mean that 100 100 plus to 102 its pointer arithmetic means we are going to move the pointer to position forward right actually it means what a means 100 plus 2 into size of the data type data type is integer into size of ray type is 4 so 100 and 8 means here now a is containing 108 now a is pointing to here now this a is pointing to here this a whatever the a in this function this pointer right two position forward we are moving one two now return a [Music] a means one zero eight we are going to return one zero eight and that would be stored in this p one zero it is what obviously address so we need some pointer to store piece pointer no problem in p also now we have one zero eight so now this p is also pointing to here now strict p value at one zero eight is three so it is going to print what three all right i hope you got the working see returning a pointer means not nothing but returning address maybe you will say here we are returning address obviously we are returning address here in a we are having address 108 you will say that we are returning address we are not returning pointer but returning address means returning a pointer same thing right say return type is obviously we need to write down a pointer so that is why we say we are returning pointer because return type of this function is pointer right but here see if i modify this thing something like this here here before calling this function here i want to write one line a is equal to a plus 2 right and after this i am just printing printf percentage d asterisk a because a is also containing base address means we are incrementing this a 2 and whatever the value after incrementing that i am going to print so what will happen please think about it what will happen if i increase a here i'm incrementing a here and i'm printing this a string after incrementing in this main it is not allowed it will give error why so because you have to you know this you have to put this point in your mind very carefully that this a the name of the array is constant pointer constant pointer to this first element of the array we cannot do increment or decrement with this a like a plus plus or a minus minus or a plus 2 something like this we cannot change the value of this a right so here it will give error but here it is not giving error why so because here it is not an array we are just writing this that it is an array maybe you are thinking here also we have something like this this a is what a means array having five elements one two three four five but no actually compiler will interpret it as a pointer just a pointer right int asterisk a just a pointer you can type you can take any other variable name b c p anything so it's just a normal pointer and obviously we can do pointer arithmetic operation p plus plus pointer plus plus pointer minus minus pointer plus two or anything so here that is why it is not giving any error but here it will give error here it is name of the array it is constant i hope you are getting my point this is very very very important point right so now let's see let's run this and see what output you will get so this is how you can return a pointer you can take float arrow also here you can take float float that's it float area you can take you just have to change the data type character we have already discussed character array means string how to return a string that also we have already discussed so now let's run this and see what kind of output you are getting so let's create a file named return pointer right now here i'm taking a function end as trick and function name is just a return pointer you can take any function name right so here i am passing i want to pass array that's it now in main what will take an actual array and i'm initializing this array here only right and one more thing we'll take one more pointer to receive whatever the function will return here i am passing what simple an array so whatever it is going to return that we will store where in p right and we will simply print that value how to print a string p using indirection operator right if you want you can also pass the length of the array right means the size of the five elements and you can check in that function also if you you know increase the length by if the length of the array is 5 and you you know in the return function in the definition you are writing a plus 7 so obviously it will be out of bound so that also you can check some kind of condition right that you can modify this program according to yourself you have to try different different variations of this program right here what we are passing so array so just accept that thing here right or in test trick a you can also write or different name you can also take right so here what i am writing return a plus 2. that is also fine or you can write here a is equal to a plus 2 and you can return simply a right now i hope it will work and let's run this and see what output will get so what output you should get i think it will print three after doing ac a plus two three at first time pointer was at one then we are incrementing by two so it will print three this is how you can win but here if you do a is equal to a plus two it will give error see let me run this and show you what what error you will get see error is what assignment to expression with array type a is equal to a plus 2 this is not allowed is what obviously array type so you cannot do a plus two right so but here a plus two is allowed so i hope you got the reason why right so that's it for this video you can try out different different programs how to return a pointer float pointer and disappointer or sometimes they can also ask like they'll give you this kind of thing like in stricken function name and anything and they'll ask you the meaning of this thing so if you have if they have written in test trick something like this it means the function will return a pointer of which data type whatever they have written in float or care that's it so i think i have occurred almost everything about function we are left with the recursion so that we are going to you know discuss in the next videos and i'll see you next video till then bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 3,649
Rating: 4.927711 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, c language tutorials for beginners, what is function in c, pointers in c, how to return a pointer from a function in c, how to return a string, gate cs lectures, file handling in c, c jennys lectures, ugc net computer science preparation
Id: RrqH9NFyprw
Channel Id: undefined
Length: 16min 41sec (1001 seconds)
Published: Fri Sep 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.