pointers jinsoft

C series for 1st sem students: Pointers as array arguments

Posted on Posted in Blogs, Technology

Pointers are used extensively in array operations. The name of the array is actually a reference to the address of the first element, rather than the actual array. Thus the array name is a pointer. So when we pass an array as an argument to a function, we are not actually passing a copy of the array (as we do in case of a variable), but we are passing a copy of the pointer to the first element of that array. This is the “pass by reference” hack that we discussed earlier.

But this phenomenon has its disadvantages as well. An array is a collection of variables of the same type. At a more fundamental level, an array is a collection of chunks of memory. The size of these chunks is the amount of memory required by the type of the variables. The size of the array, i.e. size of memory is declared while initializing the array, but if we pass the array to a function, it has no way of determining the size of the array. All the function knows is the address to the first element of the array and its type. Lets illustrate with an example.


In the program, we try to compute the number of elements of an array. We initialize the numbers array with 5 integers. In line 5, we calculate the size in bytes of the array using the sizeof() operator which returns the size in bytes of whatever is passed. Its return type is a derived type size_t which is similar to unsigned int as it has no negative values. For our learning purpose, we can assume its almost the same thing as int. Now the sizeof operator will return the number of bytes, but each element of the array has multiple bytes (2 or 4 depending on the computer). So we divide it by the size of an int variable. The output correctly gives us 5 as the answer.

Note that sizeof is not a library function, it is a built-in operator in the compiler, like other operators as +, -, &&, etc. The difference between a function and an operator is crucial in this discussion.


Now lets see what happens when the array is passed into a function, and we try to determine its size there. In this example, we create a function operate and pass the array numbers into it in line 11, of the main function.

When the numbers array is passed into the operate function, a pointer to the array is actually passed. This is because the name of an array is actually a reference to the address of the first element in the array. Thus we can say:

numbers = &numbers[0]

So when we try to find the size of the array inside the operate function, in line 4, using the sizeof operator, it throws an error/warning that it will return the size of a pointer. The function has no way of knowing the length of the array that is passed into it. So we cannot do calculations like finding the sum or average etc. on the array.


So we implement a hack. We pass the length of the array as the second argument to the function. Let us modify the operate function to find the sum of the elements of an array, and pass the numbers array and its length to this function. As we see, we have two parameters in the operate function, a pointer to an array, and the size of that array. In line 5, we run the for loop the same number of times as the number of elements in the array. Note that i and size can be compared because size_t and int types are similar. The rest of the program is simple enough to understand. We can see that the output correctly shows the sum of the elements.

C language was designed to be very close to assembly languages, although it is a high level language. Therefore we have to constantly think about memory taken up by variables. We have to plan ahead and assign memory so as not to waste RAM, which was very limited at the time when C was first developed. But there are ways to allocate memory dynamically, i.e. during execution of a C program. We’ll start discussing dynamic memory allocation from the next blog onward.


You can download the code { here }

If you liked this article please comment and show your support and interest so that I’ll be motivated to continue this effort. Like our facebook page if you haven’t already. And if you have any questions please comment. I’ll try to reply all.



One thought on “C series for 1st sem students: Pointers as array arguments

Comments are closed.