![]() In the case of a function parameter, though, we could easily delude ourselves: In fact, all these declarations for process_array() are semantically identical the code generated is the same in each case:Ī word of warning here: as we discussed above the array name yields a constant value that is the address of the first element. The array parameter degrades to a pointer – the address of the first element so we could (and many C programmers do) just as legitimately write the following and get the same result: It is not possible to pass an array by-value to a function, so the function process_array() below does not make a copy of the array: When passing arrays to functions (as parameters) things can get a little confusing. The array (‘’) notation is syntactic sugar to hide these details from the programmer: Pointer arithmetic semantics ensure that elements can be accessed as offsets from the array’s base address. ![]() As far as the C compiler is concerned an array is merely a contiguous sequence of objects (all of the same type). In C there is a close (if somewhat messy!) relationship between arrays and pointers. This week, it’s the use of arrays as function parameters.Īt first glance the code horrified me (as I’m sure it will horrify some of you out there!) but as I’ve played with it I can see real merit in the technique. Every so often you pick up a snippet of information that completely changes the way you view things.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |