How To Return A String From A Function | C Programming Tutorial

  Рет қаралды 15,613

Portfolio Courses

Portfolio Courses

Жыл бұрын

How to return a string from a function using C. Methods covered include returning a pointer to a string literal, using pass by reference (i.e. pass by pointer) to effectively "return" a string, and returning a pointer to a dynamically allocated string. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 28
@dev_ression
@dev_ression Жыл бұрын
Thanks for always making these videos clear bro!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome! :-)
@juanmacias5922
@juanmacias5922 Жыл бұрын
Got my daily C in, thanks, for the info on the basics!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You’re welcome Juan! :-)
@ramakrishna4092
@ramakrishna4092 Жыл бұрын
@@PortfolioCourses hi sir preety good explanation of the c programming strings can you tell us which ide your using ...thank you
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@ramakrishna4092 thanks Rama! I'm using Xcode on a Mac. 🙂
@kennedymunyao6136
@kennedymunyao6136 Жыл бұрын
Which IDE are using? Am a beginner in this,,
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm using Xcode on a Mac. :-)
@awekeningbro1207
@awekeningbro1207 4 ай бұрын
@12:25 when I call free after DMA, it gives me this error: free(): invalid pointer Aborted why does your works and mine doesn't?
@mauricioyepez4361
@mauricioyepez4361 Жыл бұрын
hi sir, can you make a video explaining how to reverse an array in a function , using recursion
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's a good idea for a video Mauricio, I've added that to my future video ideas list. :-) The closest things I have right now are these videos: Print A String In Reverse: kzfaq.info/get/bejne/n72BaNV4vNPPiKs.html Reverse An Array: kzfaq.info/get/bejne/bauWdJOU3MCrc2Q.html
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Here you go: kzfaq.info/get/bejne/j7GfgJemyb2ye4E.html. :-)
@yxyk-fr
@yxyk-fr 5 ай бұрын
I am not proud to have developed a different method that can return the string from the scope of the called function. It can work but it's IOCCC-level hackery... and it requires some asm to mess with the stack. hugh.
@Mnogojazyk
@Mnogojazyk Жыл бұрын
It seems there is no perfect means to return a string. Rather there are better means or more appropriate means than others, and the programmer has to know the drawbacks and compensate for them.
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's pretty much it, and a lot of things in programming are like that too. :-)
@Mnogojazyk
@Mnogojazyk Жыл бұрын
@@PortfolioCourses, indeed. In life in general, too.
@user-tn1hk6zm2freedom
@user-tn1hk6zm2freedom 4 ай бұрын
Where did you ever get car array from Character array? It is not a car array. Never ever heard of chars called cars.
@PortfolioCourses
@PortfolioCourses 4 ай бұрын
Char is not a “real word”, and “car” is actually one of the phonetically valid ways to pronounce it: english.stackexchange.com/a/60175. The people that taught me pronounced it that way and I just stuck with it too. :-)
@user-tn1hk6zm2freedom
@user-tn1hk6zm2freedom 4 ай бұрын
@@PortfolioCoursesI went to school and learned c and c++ over 20 years ago and this the first time I ever heard anyone ever call a char a car. Char is a define character not a defined car. Might as well call it a ford which would in fact be a string or maybe you would call it a rope? OMG. How things have change in this crazy world. You must be woke?
@useful_jer9659
@useful_jer9659 4 күн бұрын
@@user-tn1hk6zm2freedom What are you on about mate, sometimes people just have different pronunciations for words and it is not that deep. And I don't see how this relate to being woke.
@user-tn1hk6zm2freedom
@user-tn1hk6zm2freedom 4 күн бұрын
@@useful_jer9659 It is a char meaning character not a car.Cars have 4 wheels. Lol.
@useful_jer9659
@useful_jer9659 3 күн бұрын
@@user-tn1hk6zm2freedom Of course I know char is not car, but you're just being a dickhead by accusing people being 'woke' over such a small pronunciation issue
@victorkipkemoi738
@victorkipkemoi738 Жыл бұрын
I can't understand
@PortfolioCourses
@PortfolioCourses Жыл бұрын
What don't you understand Victor? :-) Sometimes if we don't understand something, it can be helpful to first try to understand the more basic concepts being used in the video. So if there are ideas that came up in this video that did not make sense, I may have videos covering those topics that may help, or there may be other sources on the Internet that could help you to understand those concepts as well.
@sukshithshetty4847
@sukshithshetty4847 Жыл бұрын
TestFunctionPointer(20395,0x205192600) malloc: *** error for object 0x104df3f40: pointer being freed was not allocated TestFunctionPointer(20395,0x205192600) malloc: *** set a breakpoint in malloc_error_break to debug zsh: abort I am getting this error for dynamic allocation of string within a function what needs to be done I am freeing the allocated memory in main function only
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question Sukshith. :-) I may need to see your code in order to help you, you could copy and paste it into a comment here. But based on the error saying "pointer being freed was not allocated", it looks like the wrong pointer is being used with free(). It makes me wonder if the pointer is being returned from the function correctly, or if the right pointer is being returned, etc.
Return Statement In A Void Function | C Programming Tutorial
6:45
Portfolio Courses
Рет қаралды 13 М.
Dynamically Allocate An Array Of Structs | C Programming Tutorial
15:11
Portfolio Courses
Рет қаралды 33 М.
Who has won ?? 😀 #shortvideo #lizzyisaeva
00:24
Lizzy Isaeva
Рет қаралды 56 МЛН
When You Get Ran Over By A Car...
00:15
Jojo Sim
Рет қаралды 24 МЛН
Master Pointers in C:  10X Your C Coding!
14:12
Dave's Garage
Рет қаралды 287 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 153 М.
How to return an array from a function
7:02
CodeVault
Рет қаралды 66 М.
why do hackers love strings?
5:42
Low Level Learning
Рет қаралды 399 М.
Function Pointers | C Programming Tutorial
18:31
Portfolio Courses
Рет қаралды 56 М.
Strings in C
8:50
Jacob Sorber
Рет қаралды 54 М.
Pointers and dynamic memory - stack vs heap
17:26
mycodeschool
Рет қаралды 1,4 МЛН
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
Who has won ?? 😀 #shortvideo #lizzyisaeva
00:24
Lizzy Isaeva
Рет қаралды 56 МЛН