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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
on a recent video a viewer asked if we could make a video on how to dynamically allocate memory for an array of strings in c so that's what we're going to do in this video we're going to dynamically allocate memory for an array of strings so if we wanted to dynamically allocate space for a single string on the heap it would look something like this we'd say car star string is equal to and we would call malloc and we would say size of car times length where here we're pretending that length is the length of the character array that we want to create to store the string that we're going to store into it and then size of car is the number of bytes that it takes to store a character so basically we're going to allocate space for this many characters and then string would be a pointer to that dynamically allocated block of memory on the heap now in the case of an array of strings we're basically taking this concept one step forward where we're going to have not a pointer to a character we're going to have a pointer to a pointer to a character and we could call it strings and what we're going to dynamically allocate space for initially is not some number of characters we're going to dynamically allocate space for some number of pointers to characters so really we're going to dynamically allocate memory in two steps basically at first what we're going to do is dynamically allocate enough space to store enough pointers to characters on the heap then in the second step we're going to dynamically allocate enough space on the heap to store each individual character array for each individual string so we'll go over it with an example the first thing i'll do is include a couple libraries to help us i'm going to include the string.h library because it has helpful functions like the string length function and string copy function that we can use i'm also going to include the stdlib.h library because that has the malik calic and free functions that are used to dynamically allocate memory in c so next i'll make a variable called total and total is going to store the total number of strings that we're going to dynamically allocate space 4 and i'll ask the user for that number of strings so here we'll say printf enter number of strings and we'll prompt the user to enter the number and then we'll store the number they enter into total using scanf and now we can allocate space for this number of pointers to characters so we can say strings is equal to malloc total times size of car star so what we're doing is allocating space for strings to point to this number of pointers two characters so strings is going to have a pointer to some block of memory that's able to store this number of pointers to characters and each one of these pointers to a character is going to be a pointer to our dynamically allocated character arrays they're going to store the actual string itself so next what we're going to have to do is actually ask the user for each string as we receive each string we're going to dynamically allocate space for it so we'll make a buffer to store each string that the user enters so we'll say car buffer and what we'll do is give buffer a very large size because it's really only temporary we're just basically using it to receive the strings and we'll actually dynamically allocate space for only the amount of characters we need to store each individual string after we've actually received it from the user so here we'll say number to find buffer size and we'll say 4096 bytes so that's a very large amount of space to store the input from the user we'll also create a variable called length and length is going to be used to keep track of the length of each string that we accept from the user now we're going to use fgets to accept strings from the user when we use fgets we're going to actually give it standard in as the stream from which to input the next string because the standard input stream is used for scanf and because after we enter the integer here we hit enter there's technically going to be a new line character on the standard input stream so to flush that character out we'll use this technique here we'll say wow get car doesn't equal the new line character so what this is going to do is keep on getting characters up until a new line character is encountered which is going to have the effect of flushing the standard input stream which makes it safe for us to use fgets so now we'll actually have to ask the user for each individual string we'll receive it using fgets and we'll store it into the buffer and then we'll allocate space for each individual string on the heap so we'll put a new line in first and then we'll say 4 int i is equal to 0 i is less than total i plus plus so we're having this counter go from zero up into total by one because total is the number of strings that we want to create on the heap and then we'll prompt the user for the string so we'll say printf enter string percent d and we'll say here i plus one so we can ask them enter string one enter string two enter string three and so on next we use f gets so we'll say f gets buffer buffer size stdin for standard input stream so what we're doing here is accepting input from the user on standard input which is going to be the terminal or the console by default and it's going to be stored into buffer up to buffer size amount of characters and the reason again why we had to flush any characters from the standard input stream especially the new line character is that if we didn't do that there would still be a hanging newline character on the standard input stream from scanf and in that case for the first string f gets would see that new line on the standard input stream and it would basically store a blank string into buffer it actually wouldn't even pause for user input so that's why we have to flush the standard input stream once we've done that once though we can just use fgets like this and we're good so that will give us the string now to get the length of the string we'll say length is equal to str len buffer so the string length function will give us the length of the string that the user entered and that was stored into buffer not including the null terminator character and we'll store that length into length because we're going to use it to dynamically allocate space for the string that we're going to store on the heap so one thing we're going to do is with f gets if i were to enter the string abc and hit enter then buffer would store a string that looks like this abc backslash n for the newline character so fgets will actually pick up and store the new line character that the user enters when they hit enter to terminate their input fgets will actually store that enter character which is effectively a new line character into the buffer itself we don't really want that we don't really want that last new line character so what we're gonna do is actually move up the null terminator by one in buffer to just sort of eliminate this new line character so we'll say buffer at length minus one is equal to backslash zero here and this is the special null terminator character that terminates the string and what we're doing is we're moving it up one character in the string which effectively puts it here and then the string gets terminated without that new line character being there so next what we'll do is actually dynamically allocate space for this character array on the heap so we'll say strings i is equal to malloc length times size of car so remember here that strings is not a pointer to a character it actually has on the heap an array for storing pointers to characters that's what it's storing is an array of pointers two characters and what we're saying here is take the pointer to a character at index i and have it point to this block of memory that we're allocating and we're allocating a block of memory that's going to be large enough to store this number of characters length number of characters now normally if we're allocating space for a character array we might have something like this length plus one to account for the null terminator in this case here we've actually already shaved off one character in our string that newline character so we can actually just use length and length will include the null terminator and we're actually okay and next what we'll do is copy the string from the buffer to this newly dynamically allocated space so we'll say str copy we'll call the string copy function and we'll copy into strings at i what's in the buffer now and that'll actually copy the string into that space that we've just dynamically allocated so now we've done it and now we can actually loop through this array of strings and print out each string just to verify they're there so we'll say printf backslash n resulting strings array colon put in some new lines and we'll say four into i is equal to zero i is less than total i plus plus and we'll print out each individual string so we'll say printf strings at percent d is equal to percent s new line i and strings at i so we'll put the index here and we'll put the string itself as well and we'll also just throw in one more new line on the end there okay so we'll save and compile and run this and we'll see how she goes so build succeeded says enter a number of strings we'll say five enter string one i'll say abc space one two three add a string two i'll say this is a test enter string three i'll say i'm from canada enter string four i'll say this is another test and enter string5 i'll say it's sunny outside and the resulting strings array looks like this we have those strings stored into the array of strings there this is how we could dynamically allocate space for an array of strings in c checkout portfolio courses.com where we'll help you build a portfolio that will impress employers including courses to help you develop c programming projects
Info
Channel: Portfolio Courses
Views: 44,195
Rating: undefined out of 5
Keywords:
Id: 4_2BEgOFd0E
Channel Id: undefined
Length: 12min 9sec (729 seconds)
Published: Fri Jan 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.