Recap: Pointer Array Traversal
Part of the Logic & Flow section of Coddy's C journey — lesson 11 of 63.
Challenge
EasyWrite a C program that implements a function to calculate the sum of all elements in an integer array using pointer arithmetic and array-pointer equivalency. Your program should:
- Create a function named
calculateSumthat takes two parameters:- A pointer to an integer (
int *arr) representing the array - An integer (
size) representing the number of elements in the array
- A pointer to an integer (
- Inside the
calculateSumfunction:- Initialize a variable
sumto 0 - Create a pointer variable
ptrand set it to point to the beginning of the array - Use a loop to traverse the array using pointer arithmetic
- In each iteration, add the value pointed to by
ptrtosum - Increment the pointer to move to the next element
- Return the calculated sum
- Initialize a variable
- In the
mainfunction:- Read an integer
nrepresenting the size of the array - Declare an integer array of size
n - Read
nintegers from input to populate the array - Call the
calculateSumfunction with the array and its size - Print the result in the format: "Sum: [value]"
- Read an integer
The function must use pointer arithmetic for array traversal - do not use array indexing notation like arr[i]. Instead, use pointer dereferencing and increment operations to access array elements.
For example, if the input is:
5
10 20 30 40 50Your output should be:
Sum: 150This challenge tests your understanding of how functions can accept arrays as pointer parameters, how to use pointer arithmetic to traverse arrays efficiently, and how array names are automatically converted to pointers when passed to functions.
Try it yourself
#include <stdio.h>
// TODO: Write your calculateSum function here
int main() {
int n;
scanf("%d", &n);
int arr[n];
// Read array elements
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// TODO: Call calculateSum function and store the result
// TODO: Print the result in format "Sum: [value]"
return 0;
}All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions