Arrays as function parameters in C

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i want to take a look at a very simple concept that many of uh us probably encountered earlier uh when we started learning c and that is passing arrays as parameters to other functions how does that work and why is it so confusing uh compared to other languages first things first let's define here a uh an array let's say array of five elements sure i'm gonna have one two three four five in here simple enough nothing too fancy and uh let's try to i just want to i just want to print out the size of this already we know that with arrays what you can do is you can just say size of array and it will give you the number of bytes this array takes up right um i can just print out this size size of the array let's say in main percent llu backstage and i'm passing this size of s parameter here let's see just like that now if we try to launch this well we're gonna get 20 and 20 is 20 bytes represents 20 bytes and that is because we have five integers and since each integer takes in four bytes on my computer then that's five times four which is 20. so it's to be expected it's it's the proper result but what happens when we do the same with uh inside another function by passing this array as a parameter so if you write here a function saying print size and we're passing as the parameter here array and we define it like so we say um array parameter and it's going to be an array of five integrals and then we do the same exact thing as we did up top or down here in the main function we just copy in this and we say size of the array in well the same print in function let's just call it like that okay and of course this is going to have to be array as param sorry what's going to happen now what's going to be the result well if we try to call this function so print size of our array just array nothing else because we know that array of five is actually an array of five integers so that should be fine right uh what's gonna happen if i launch this well if i try to launch this you're gonna see something interesting that the size of the array in the main function is the proper size we have 20 that's five times over bytes that's perfect but in the function we actually get eight what what gives did we lose three integers all of a sudden and something happens happen with those three not quite not quite the actual issue is that what we're passing here whenever we try to pass an array to a function that array actually decays to a pointer to a pointer to whatever type this array is so since we have an uh an integral array this guy is going to decay to an integral pointer right so array decays to an endpointer now the c language does let us define the parameters as arrays but it's going to treat them exactly the same as if we had an int pointer here so this again really is just this is exactly the same thing nothing different about it so now if we take a look at the printf it's kind of obvious why we're getting eight because this parameter is of type in pointer and we know that pointers are eight bytes at least when we compile it to 64 bits so if we try to launch this of course we're going to get 8 here because this decade into a pointer was passed here was considered a pointer and then it just printed out the size of that pointer which is of course eight bytes so whenever you want to pass in a uh one dimensional array to a function you're better off just defining the parameters as pointers always right because that's less confusing and usually or all the time they're going to be treated as pointers so might as well have them they are uh in the same way as it's being used okay then we know that the sizeof is going to give a different result and we can notice that this actually does convert or decay into an int pointer all right now uh with this in mind whenever you want to pass in arrays to functions you should actually also pass in the size of that array so usually i just pass in a size t size and say here five so that in the function if you want usually you want to iterate an array if you pass it to the function if you want to do that we can actually iterate over it so that's fine another confusing thing about uh defining the parameters the array parameters as this in a function is that you would assume that this is an actual array passed to the function so a copy of the past array so uh you would assume that okay well this is going to create a copy a local copy for this function that you can then modify without modifying this array but in actuality this doesn't happen if you change the array here if say array of zero equals i don't know 10 and suppose we actually print it here printf percent d backstage and array of zero after calling print size you will notice actually i have to type in here array param of course you will notice that of course it does get modified right so really don't use this definition of parameters just pass them as a pointer and then it's obvious that of course since the pointer it does modify the whatever underlying uh part in memory it's looking for now one more thing regarding this whole issue is when you're dealing with multi-dimensional arrays so if you have here for example a 2d array so let's say we have a 5x5 array let me define it real quick here just like so what does this actually decay into does it decay into a double pointer now we have a 2d array does it decay into something else well when you're dealing with 2d arrays well it's it's a bit more trickier to pass them as parameters to other functions in this case here we cannot simply pass in a simple pointer because now if we try to compile it we're going to get all sorts of in this case just warnings but a very important warning saying that it was expected an int pointer this in pointer but argument is of type int pointer to an array of five integers what that what does that mean well this array now decayed instead of into an in pointer it actually decayed into an int pointer to file that means an a pointer to an array of five integrals but in this case if you actually change this parameter to be on uh pointer to an array of five integers well remember that c doesn't care about arrays in the parameters of a function so it's going to again be converted into a pointer so this of 5 is just going to be removed and a rasterist will be added here but this is actually a problem because what we have here is not in any way sort of form a double pointer we have an array of five arrays of five integers that's it and well if we were to have this as a double pointer saying something like array param of zero zero so accessing the first one here right and let's say i assign it to 10 this is gonna actually crash our program now why is that uh that is because of the way arrays are treated in c sorry that kind of tree specially but with pointers that that doesn't uh doesn't fly so array here gets treated as a double pointer but it is actually just a simple pointer simply because if you do array param of 0 well that is the first element the first integral at this pointer and if we dereference this guy once we are going to get the first hint in the fourth array right but when we say this again right of zero again well that's invalid why because this gets evaluated to 1 and we try to dereference the address one and of course that just breaks everything so you cannot do that with arrays inside of the main function right array of zero zero equals ten you can do that simply because they are actually uh arrays they are not pointers they are not double pointers or anything like that and this is treated sp more in a special way so what what can you do in this case well if you really want to pass in on a 2d or multi-dimensional array well you have to pass it as this but you're going to have to convert it into an end pointer right so convert it into an in pointer get here just a simple pointer to our integrals and when you want to change something so let's say i want to change oh i don't know uh i want to change on the third the third uh array i want to change the first element well what i can do is simply say off two times five right if i say or of two times five well that's gonna go ten integers down the line so it's gonna skip over the first two arrays and access this one and if i want for example the third element i can just say two times five plus two in this case and if we print f here let's say percent d backstage and array off well two times right that's two and of two we're gonna see that we get 10 as a result in here right so this is how you would uh juggle around with multi-dimensional arrays that are being passed as parameters to other functions i know this is a bit more complex but uh really this is the way to go because of that weird quirk whenever we define arrays inside function parameters they do get converted to pointers but of course as i said this array cannot be treated as a double pointer because this is all there is in the array if we de-reference it once that's it that's going to be a number right and if we try to de-reference it a second time it's just going to data from some address 5 and of course 5 probably is some protected place in memory it's going to break right so in here we have to treat it as such but in here if you have an actual array and ar r is an array identifier is not some sort of function parameter then this doing this is specially treated and it does actually work but behind the scenes the program probably does something uh that something like we did here okay it's just one the reference is not actually two okay and that's about it for today i know this was a lot but again as a conclusion uh if you want to pass in just a one dimensional array to a function just uh define the parameter as a pointer and work with it as such if you want multi-dimensional simply just have a single pointer to that array and dereference it like this inside the function but you can also deference it like this inside the main function where this array is defined okay i hope you got something out of this video if you do have any questions leave them down comments below or on our discord server again the source code for all this will be found down in the description below on our website take care bye you
Info
Channel: CodeVault
Views: 2,534
Rating: 5 out of 5
Keywords: codevault, c (programming language), arrays, parameter, function, pointer, 2d array
Id: Cfm4D_Mxpiw
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Mon Jan 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.