Dynamically Allocate Memory For An Array Of Strings | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

HOW TO FREE THE MEMORY:

/* free each string pointer in a loop */
for (int i = 0; i < total; i++)
free(strings[i]);

/* free pointer to strings */
free(strings);

PortfolioCourses
Автор

These are great C tutorials. I’ve probably written over a million lines of C code over the last 35 years, and this series hits all the important points a beginner needs to learn to become proficient in C.

mensaswede
Автор

Man i was so confused by this topic you just rocked thx

technicalgamer
Автор

This is exactly what I need for my Cs exam

withthehelp
Автор

nice bro! its very kind that ur helping ur subs

eighteen
Автор

You solve my problem! Lots of thanks🧡🧡

MARCBOSCHMANZANO
Автор

You are very impressive
God bless you
I teach basics of c, free to rural students
Your videos are quite helpful
🙏

arasunatesan
Автор

thanks for helping me finish my project for this semester ✅

sergioberezovski
Автор

The most important is at time frame @11:34 from Canada LOL - nice video. Thank you Kevin.

fifaham
Автор

Amazing, cleared all my doubts and even taught me new stuff! Thank you so much

ninadgandhi
Автор

Thanks a lot!

I was confused by the getchar part (I thought getchar() does not read '\n' in the course of while loop! :). Then I read some articles on the internet and noticed, getchar() reads all the characters up to and including '\n'. Basically getchar() is called before even it compares the character with line feed. So, at the end, the '\n' has been read (and therefore flushed), the returned character, '\n', is compared to '\n', and we exit the loop.

kuijaye
Автор

For the love of god i couldnt figure out how to do this. Thanks god u made a video for this. Just TAKE MY THANKS LIKE SUB

prumchhangsreng
Автор

Excellent job. You should also show similar version where reading from a file.

educationandmore
Автор

after watching few videos of this channel. i got some super powers

patchavavengalraovengalrao
Автор

I prefer the "direct" allocation syntax, as it seems to be less error prone. I mean, by using the dereferenced pointer as an operand with the sizeof keyword:
p = malloc(how_many * sizeof * p);
Let's say the char is now wchar_t or something, no need to worry about missing all these.
Will not work with cpp as far as I remember though.

rotemlv
Автор

Error at 0:58, where you 'malloc (sizeof(char) * length)' for a string of length bytes.
You either forgot to mention, that the length MUST include the trailing '\0' or you should
'malloc (sizeof(char) * (length+1))' in order to add the required one byte for the trailing zero-byte.

Forgetting to allocate the additional byte for the trailing zero is a common mistake among beginners,
and it leads to a certain amount of frustration as these errors usually do not show up immediately.
That's why it cannot be emphasised enough.

Rai_Te
Автор

THANK YOU!!!! I had not realised it was this simple! I had always been confused by the fact that the double pointer is simply an array of pointers and does not contain the strings themselves! Just one question, could you use realloc() on the double **ptr to dynamically resize the array of strings (say, if you don't know from the start the number of strings that will be stored)?

Walkingdeadman
Автор

thanks for this tutorial! Can you also make a video on how to cut strings before a symbol/character, then returning it to main, printing it on the terminal and saving the new string to a file? I’ve been racking my brain on how to cut an email address string before the @, then return and save it on a file, but i keep getting weird symbols with it

brycenava
Автор

great video!!thank you! i still have a question.. so if strings[i] points to the i'th string then how should i go to access a specific word from the string ? something like strings[i][j] ?

aliali-ggxu
Автор

Thanks you
Could you demonstrate an example of reading files into a dynamic memory and printing them. Couldn't find anything useful

ahmadalwazzan