What is a 1D Array?
A 1D array in C is a collection of elements (usually of the same data type) stored in a continuous block of memory. It’s like a row of boxes, each holding a value.
Example:
int marks[5] = {85, 90, 78, 92, 88};
Here, marks is a 1D array with 5 elements.
Why is Passing Arrays Different?
When you pass a normal variable (like an int) to a function, C makes a copy of it.
But when you pass an array, you’re actually passing a reference (or pointer) to the first element of the array. This means the function can access and even modify the original array.
How to Pass a 1D Array to a Function
Method 1: Using Array Notation
You can pass a 1D array using regular array notation in the parameter. The function receives a pointer to the array’s first element.
Syntax:
void functionName(dataType arrayName[], int size);
Example:
#include <stdio.h> void displayQuizScores(int scores[], int size) { for (int i = 0; i < size; i++) printf("Quiz Score[%d]: %d\n", i, scores[i]); } int main() { int quizScores[] = {85, 78, 90}; displayQuizScores(quizScores, 3); return 0; }
Output:
Quiz Score[0]: 85 Quiz Score[1]: 78 Quiz Score[2]: 90
This C program prints quiz scores from an array. The displayQuizScores function takes an array and its size, then uses a loop to print each score with its index. In main, the array quizScores has three numbers. The function is called to show all the scores.
Method 2: Using Pointer Notation
You can also pass a 1D array as a pointer. This gives the same result as array notation, since the array decays into a pointer.
Syntax:
void functionName(dataType *pointerName, int size);
Example:
#include <stdio.h> void displayTestScores(int *scores, int size) { for (int i = 0; i < size; i++) printf("Test Score[%d]: %d\n", i, *(scores + i)); } int main() { int testScores[] = {68, 74, 82}; displayTestScores(testScores, 3); return 0; }
Output:
Test Score[0]: 68 Test Score[1]: 74 Test Score[2]: 82
This C program prints test scores using pointers. The displayTestScores function takes a pointer to the array and its size. It uses pointer arithmetic to access and print each score. In main, the array testScores has three values. The function is called to display them.
Method 3: Specifying Array Size in Parameters
For clarity, you can specify the size in the function parameter, though it’s mainly for documentation—C still treats it as a pointer.
Syntax:
void functionName(dataType arrayName[fixedSize], int size);
Example:
#include <stdio.h> void displayCertScores(int cert[3], int size) { for (int i = 0; i < size; i++) printf("Certification Score[%d]: %d\n", i, cert[i]); } int main() { int certificationScores[] = {91, 88, 95}; displayCertScores(certificationScores, 3); return 0; }
Output:
Certification Score[0]: 91 Certification Score[1]: 88 Certification Score[2]: 95
This C program shows certification scores stored in an array. The displayCertScores function takes the array and its size, then prints each score with its index. In main, the array certificationScores has three values. The function is called to display them.
Practical Example: Modifying Array Elements Using Function
#include <stdio.h> // Function to double the values in the array void doubleMarks(int marks[], int size) { for (int i = 0; i < size; i++) { marks[i] = marks[i] * 2; } } int main() { int testMarks[] = {40, 55, 60, 72, 48}; int n = sizeof(testMarks) / sizeof(testMarks[0]); printf("Original Sanfoundry Test Marks:\n"); for (int i = 0; i < n; i++) { printf("%d ", testMarks[i]); } doubleMarks(testMarks, n); // Modifies array elements printf("\nDoubled Sanfoundry Test Marks:\n"); for (int i = 0; i < n; i++) { printf("%d ", testMarks[i]); } return 0; }
Output:
Original Sanfoundry Test Marks: 40 55 60 72 48 Doubled Sanfoundry Test Marks: 80 110 120 144 96
Explanation:
- The program defines an array testMarks with five test scores: 40, 55, 60, 72, and 48.
- It calculates the number of elements in the array using sizeof(testMarks) / sizeof(testMarks[0]).
- The original marks are printed using a for loop.
- The doubleMarks function takes the array and its size, and doubles each value using a loop.
- After the function call, the updated (doubled) marks are printed to show the effect of modifying the array through a function.
Best Practices
- Always Pass Array Size: When passing arrays to functions, also pass the size of the array to avoid out-of-bounds errors.
- Use Meaningful Function Names: Function names should clearly indicate their purpose (e.g., calculateSum, findMax).
- Avoid Hardcoding Sizes: Instead of hardcoding array sizes, use sizeof to determine sizes dynamically.
- Use const for Read-Only Arrays: If a function doesn’t modify the array, declare the parameter as const to prevent accidental changes.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Practice Computer Science MCQs