C_98 Return a String from a 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 the previous video we have discussed how to pass a string to a function in c now in this video we'll see how to return a string from a function see this this concept is very important and you can say it's very confusing i'm damn sure 90 percent students are not able to answer this question in their b tech time like how to return a string from a function and if you can answer this question or the the further question is what if you want to return if you have returned a string but i want to modify that string also in main function then how you can do that so under this topic returning a string of from a function there are many important point under this concept but you can say important sub concepts all the things all the important points we will discuss with proper program and i'll show you practical also so this is very important uh you can say uh from the point of view of interview and also in why was they asked these type of tricky questions so this is a little bit tricky and confusing so you have to pay attention on this topic right but before starting just want to 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 the questions being asked in the interviews what is the thought process behind the great application like zometo ola google amazon how to improve your culture ranking so here an academy brings you a platform where you can get access to weekly shows which you can watch like and the host of these shows are working in some of the top companies like linkedin google amazon and our seven star coders on codeshare and industry experts with years of experience so they'll be covering the content which would be really helpful for your career like in the live episodes you get an opportunity to ask take a charge about the top 20 questions being asked and the industry leaders directly about the recruitment process in top startups and mncs and what is the eligibility criteria and how to apply for them and you can also get your resume reviewed by the experts stes not only this you can also participate into mock interviews and you can learn courses on programming languages blockchain and crypto cloud computing take aspects of digital marketing and data analytics and there's one more show in which the host will help the users to understand how the applications we use on our android phones are made and in these sessions he'll take the learners through how to get started with the native android development in detail and this show consists of learning java language for android and building blocks of android studio ide to make native android apps 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 percent extra discount on your paid subscription so now let's see how to return a string from a function see simple i am just taking a simple example so here if i want to define that function right so how you will define that function here i am defining that function like obviously first of all if you are returning a string there must be some return type right the name of the function name of the function i am taking display right and here i am not going to pass anything means the function should return something but don't take any argument that kind of function i am going to write here right so here here suppose i am writing what return and string is what jenny this is obviously string or more specifically if i say it's string literal right so i'm returning this string literal so what should be the return type that is you can say a confusing type of question right what you will write here see here we will write string is what nothing but character array right so can but we cannot simple write care because it is character array and care means it it will return only a single character but it is going to return a string so ultimately what it will return if you know the behind the scene process it will return the pointer to the first character of the string pointer to the base you know base address of this string or character array so just you will write down care as trick simple you are not returning jenny completely we are just returning what pointer to the first character of the string and that's it using that pointer we can access the string yes or no are you getting my point so i hope you know why you will write here cares trick it is a pointer pointer to character right and using this pointer obviously we can access the complete string so this it will return this pointer to wherever you call this function suppose in main we are calling this function right in main i'm calling this function uh right here i'm writing main and here i'll call this function right so i'm calling this like display but it will return something so you have to accept that thing so what you will write here here also it is returning pointer to this so here also you have something the this kind of thing only to accept whatever it is returning so here i am taking suppose here i am declaring one care asterisk str a string something like this you can declare a string something like this also or care str and size you i can say 20 this whole so this is also fine but yeah there is a difference how the memory would be allocated to this string how the memory would be allocated to the string that is different thing means how these strings are to be stored in memory that is different thing right so this is also very important concepts in c how the string literals are stored in memory that is very important and that is very confusing but if you pay attention you will get easily that also will discuss in a separate video right how these string literals are stored in memory so now here something like this and you can simply write here str so this str whatever it will return the steer will store that thing and you can simply print printf and string is percentage s and str that's it now obviously declaration is also there for this function so how to declare return type is castric and the name is display and that's it we are not passing any argument right but there are many many things here this is not it this is not done one by one will discuss all the points so now see whenever control will go to the main function will execute this means here for the main one stack frame would be allocated and here we have str a pointer right here this time it is having some garbage value now here we are calling this function so control will go here so for display function also what memory would be located right now this is what you can just stack and this is what the stack frame for this display so now return jenny here we are having this string gen so this would be stored in memory right so now how this would be stored in memory here this string would not be stored in the stack memory of this function i hope you know that like the memory would be like one is a stack one is memory heap memory and some some you know memory is for you can say static or global variables and sum is for that code or the text whatever you write that is how the memory would be divided right so whatever the thing in the stack the variable declaration or anything in the stack that would be destroyed once control will go out of this function but if you have used the heap memory for storing something then after returning the control from here but still the heap memory will not be cleared right no generally from the heap when memory would be allocated dynamically that we call as dynamic memory allocation another dynamic memory location if you use malloc and calo means the memory would be located from the heap area not the stack area by default if you here take any variable in you know a b or you can say care abc something like this by default those variables are stored in step so once control will go out of this function stack memory would be vanished right so now this is also string literal and this will be stored in a you can say separate its kind of separate memory section which is read only you cannot modify this string right so this would be stored in a separate you can say i know memory allocation j e double and y and null but this memory is read only memory you can only access this but you cannot modify this right so now return jenny so once we will see the return statement control will go back here but still this memory is what it's like you can say a static memory section static means the lifetime of this is throughout the program it's not like that once we go back this will be finished there is no jenny now no still you can access this right so now once we go back here what it would return suppose the first character addresses hundred one zero one one zero two one zero three one zero four one zero five so it will return hundred right the pointer to the address of the first character hundred now in str also we have hundred so now it is pointing to this and now simply you can print this str right second way is if you do something like this see rather than writing this i'm writing taking care str something like this engine and return str this is also same thing i am now initializing string something like this taking a character array name str i'm initializing it and i'm returning this str so now at this time if you initialize a string if you declare a string something like this now how this jenny would be stored the in the stack area of this function in the stack memory of this function not the heap memory or not in a separate memory section right in the stack so here in the stack frame suppose we have jenny something like this and null and the address is suppose i am taking 200 201 202 and something like this right so now str is containing obviously str is what here also we have str and str name of the string this is containing good address 200 this i hope you know address of by default this act is a pointer which contains base headers of this character array so 200 is it is containing so we are returning str str means we are returning 200 so now this str will store 200 and now we can display this this is also fine right but now it will not work see why it will not work see maybe you are thinking last time it worked but this time it will not work why because this stack memory now this time this jenny has been stored in the stack memory of this function display function so now once control will go after returning here means this memory has been freed has been vanished so now you cannot access the original value of this str that is jenny whatever this str is containing jenny in the main function or in any other function where you are calling this so now what it will print null or any garbage value it can print right because this is what local to this function this time if you declare something like this it is local to this function because the memory has been allocated from the stack area so now what you can do the solution is one thing what you can do you can write down here static static means now static means now this this lifetime of this is what throughout the program right for static and global variable there is a separate section and those static and global variable you can access from throughout the program so now this time it will print gen now the memory would not be allocated from the stack part of this function only no separate memory allocation static means for static and global there is a separate memory allocation now where at that place danny would be stored and you can access jenny from here also right or another thing is if you will not do if you will not write static here what you can do this is one another way of declaring string is would care asterisk str this you can also you can do because obviously in str a star is a pointer it will contain base address of this so that is why i am just declaring str as a pointer and pointer will contain the if you write something like this it still will contain means it means i am initializing this pointer with base address of this character based address of the string literal that's it and i'm returning this str now this time this time also no need to write down static here this time this pointer the value this pointer is pointing to that would be allocated some space from the heap area so this jenny now stored not from the stack area of this function in the heap area and that heap area whatever the heap is in the c period that would not be cleared after returning from this function so that is why when we return from this function still gen is there and you can access that jenny using this pointer str whatever it will return the address of the first pointer here also that is also fine right but here the problem is see the problem is if i want to modify this thing here suppose i have returned this jenny jenny has been printed but i want to after returning after returning here before printing or after printing also you can take so here i am writing here i am writing i am modifying this i am accessing str of 0 first first character str of 0 z rather than j it should be z right but now see at this time if you write something like this right so it will give it will not give error it will give you can say segmentation fold type of thing it will not give compile time error but at runtime your program will crash either your program will crash or it will not print anything right if before this printf you are writing this and after you are printing it will not print anything its kind run time exception or error you can say this kind of thing you will not get any compile time error why so we cannot modify this because because now this if you declare something like this now this string literally has been stored i have told you enough you can say that section of memory which is uh it is read only but here you are not getting any compile time error only your program is going to crash so that is a big issue i want if someone want to modify the string here it should give compile time error so what you can do what you can do here you can declare it const const means now everybody knows that it is not modifiable so now here also you have to declare it const care style if you only declare its conest here and here not means while returning this return statement will discard this const right it is of no use so you have to declare hair also const now if you will modify if you want to modify this it will you compile time error your program is not going to crash it will come we will compile time error to you that is a good idea now you have handled that issue right and here also you can write cones care asterisk display this function also you can write down here const now what the thing you want you want to return a string and you want to modify that string also at that time what you will do if you declare something like this you cannot modify it because in this declaration and initialization this jenny has been you know allocated from the memory which is read only read only memory so you can write down something like this now this gene would be stored in the stack part of this display and you can modify it also it is modifiable right so rather than writing here what what i can write static right now return str and here you can modify it and you can print it so i hope you got all this thing yeah its little bit confusing but i will run it we will run it and we will see what kind of thing you are getting right so you have to practice of returning a string from a function so string in a cr considered as character array so we cannot return a string actually right what you can return the pointer to the first character of the string you can see the address of the first character of that string that thing you can return that is why i guess you know now you go to the why the return type of this function is characteristic pointer to character type right so now let's run this and see what kind of thing you are getting so now let's create a new file return spring right and here we are using what only one function that is display will not pass anything we'll just see how this function will return a string so in main what we are taking cares trick and str right so now what you can do is equal to take nothing and whatever this display will return this str is going to hold that thing and it just will print the value so string is percentage s and str that's it now what the definition part here i am taking first example is simple this jenny return journey right now let me run this and see what kind of output you are getting so say string is jenny it is returning the string and suppose you want to modify this type of string str 0 at the place of 0 i want that it has z and after that i'll print z e double n y so now let's see what is going to happen see it is not returning anything because we cannot modify this string it is a it is only read only the string is only you know we can read only we cannot modify this now see next is what i'm not going to return something like this here i'm going to take care str and gen right and this time i will return str only fine so now if you declare a string something like this now it will take memory in the stack part of this display function right so now that stack memory will be cleared once the control will go out to this function so now it will not you know print anything or it will print any garbage value you can say now see let's check what it is printing see okay now we are printing something like this see the warning is what in the display the function return address of a local variable right local variable local variable means that variable has been by default those variables would be you know will be taking place in the memory which is stack you can say from the stack part of this this display function so that will would be considered considered as local to this so it is not going to print anything right now see one way is you can make it steady right and we will not modify this first we will just display the string now this time it is printing string is gen because now static means in a separate memory location it has been the gen is being stored that is what you can say that memory allocation you can access through throughout the program it's like global and if you want to modify this now what will happen see now what it will print see z e double and y you can also modify this right if you declare it something like this using this method but now if i declare it something like this here this is another way for declaring a string care strict str jenny and i will not modify it first we'll just display this one we are not using any static keyword here but still it will print jenny see string is because now the memory has been allocated from the what heap area of this function not the stack area and that heap area you can access where you are calling it means that whatever the in the heap area that would not be cleared once control will go out of this thing but you cannot modify it you can say that memory section is what it's kind of for read-only yeah you can access it where you are calling it right that is global you can say considered as you can you can say that you can access it throughout the program but that would be it's kind of separate memory allocation for string literal and that is only read only you cannot modify that thing you can access the string but you cannot modify so if you will modify it see what you will get see it is not printing anything right and if you modify it after printing the string right i am modifying it str 0 equal to z and again i am printing this after modifying also i want to print now see what kind of thing you are getting you will only get string is jenny only one printf would be executed not this this one right or sometimes in some compiler you will get segmentation fold type of thing and the program would hold means it would break you will get some undefined behavior so you know to remove this kind of thing you know we want that it should at compile time only it should give error that you cannot modify the string right so what you can do here what generally we will write here const keyword right and here also const right now see now we are not modifying it we are just printing see what it will give string is jenny it is printing string is jenny right and also it is giving warning likes return discard const qualifier from the pointer target type because here return type is only characteristic it's not const so here you have to write down const to get the error now see okay sorry in declaration also obviously in definition you have write down this const so here also in declaration also it should be const right now let me run this and see you are getting string is jenny and now if you want to modify this and again i want to print at this time it will give error see assignment of read-only location asterisk str here it is giving error in this line this line right because it is only read only location str where the journey would be stored at read only location only see read only location assignment of read only location here so better to write down this cost keyword here and if you don't write down here the cost give a return type here const care star then also it will discard the const right so better to write down here what const return type of this type of function would be const care asterisk right now see and see if you just write down return type of this is const but here you are not writing in the main because str is accepting whatever this display would be return so here if you will not write const at this place then what it will discard response it is printing string is jenny and warning is what assignment discards cost qualifier from the pointer target type it will discard this const so here also you have to write down the sconced keyword right now it will return what cons type of pointer so it will accept also that return type should also match const care s3 at this time you will get error this program will not run right see see assignment of read only location str this is now read only location and we are we want to modify this location so it will give compile timer so we have handled this issue i guess right so i guess you got how to return a string how to return a modifiable string means you are more to return a string and you want to modify that also and if you want to just return a string and you don't want to modify that string and if you want to if you modify that string in main function then it should you compile time error that also we have discussed right so you have to try out different different things you know how to return a string right and if you you know if you get any error you can ask me in comment box so i guess that's it the basics of this how to return a string that is what basic right so in the next we will see how to pass a pointer to a function pointer as an argument to a function here i am going to pass a pointer while calling right so now in the next video till then bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 4,805
Rating: 4.9170985 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 programming tutorials for beginners, what is function in c, how to return a string from a function in c, how to pass string as an argument in c, what is array in c, pointers in c, gate cs lectures, classification of functions in c, ugc net
Id: JNdbsF-78uk
Channel Id: undefined
Length: 29min 7sec (1747 seconds)
Published: Wed Sep 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.