Tamil Technicians – C Programming Course · Pointers with Arrays & Functions
Pointers with Arrays, Strings & Functions – Real Use Cases
In earlier lessons we saw basic pointers, arrays, strings and functions separately. But in real C programs, these four concepts are almost always used together.
In this lesson, we will connect them and see:
- How arrays and pointers are related (array name as pointer).
- How to use pointers to walk through arrays efficiently.
- How
char*strings work and how to use string functions safely. - How to pass arrays and pointers to functions (real “use cases”).
- How pointer-based functions can process and modify data in the caller.
1. Arrays & Pointers – What “Array Name is a Pointer” Really Means
You may have heard the sentence: “Array name is a pointer to its first element.” That statement is a simplified way of saying:
In most expressions, an array name (like a) decays into a pointer
to its first element. So a behaves like &a[0].
Simple example
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
printf("a = %p\\n", (void*)a);
printf("&a[0] = %p\\n", (void*)&a[0]);
printf("a[0] = %d\\n", a[0]);
printf("*a = %d\\n", *a);
return 0;
}
Typical output (addresses will differ):
a = 0x7ffccd3000
&a[0] = 0x7ffccd3000
a[0] = 10
*a = 10
aand&a[0]show the same address.*ais the value at that address →a[0].
However, remember this important difference:
ais not a normal variable. You cannot doa = someOtherPointer;.int *pis a pointer variable. You can assignp = a;,p++;, etc.
2. Using Pointers to Iterate Through Arrays
With arrays, you are used to writing:
for (i = 0; i < n; i++) {
printf("%d\\n", a[i]);
}
The same loop can be written using pointers:
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *p = a; // points to a[0]
int i;
for (i = 0; i < 5; i++) {
printf("%d\\n", *(p + i));
}
return 0;
}
Or even this variant:
#include <stdio.h>
int main() {
int a[5] = {10, 20, 30, 40, 50};
int *p = a;
int *end = a + 5; // one past the last element
while (p < end) {
printf("%d\\n", *p);
p++; // move to next element
}
return 0;
}
Pointer arithmetic recap
- If
pisint*, thenp + 1moves bysizeof(int)bytes. - If
pisfloat*, thenp + 1moves bysizeof(float)bytes.
3. Modifying Arrays Using Pointers
Pointers are not just for reading. You can also modify the array elements through a pointer:
#include <stdio.h>
int main() {
int readings[4] = {100, 105, 110, 120};
int *p = readings;
int i;
// Add calibration offset of +2 to all readings
for (i = 0; i < 4; i++) {
*(p + i) = *(p + i) + 2;
}
for (i = 0; i < 4; i++) {
printf("%d ", readings[i]);
}
printf("\\n");
return 0;
}
Here we treat readings like a sensor calibration list. Using pointers, we efficiently
adjust each value in place.
4. Strings & char Pointers – Real C Style
In C, a “string” is just an array of char ending with '\0' (null character).
The most common way to work with strings is through char pointers.
String as array vs pointer
char name1[] = "Tamil"; // array, stored locally
char *name2 = "Technicians"; // pointer to string literal (read-only)
name1→ array that you can modify:name1[0] = 't';.name2→ pointer to read-only memory in many compilers; do not modify:name2[0] = 'T';is undefined behaviour.
char* pointer (e.g., char *s = "abc"; s[0]='x';).
Use a char array if you want to edit the contents.
Walking through a string using char*
#include <stdio.h>
int main() {
char msg[] = "Hello C";
char *p = msg;
while (*p != '\0') {
printf("%c ", *p);
p++;
}
printf("\\n");
return 0;
}
This is very similar to walking through an int array, but the stopping point is
the null character '\0'.
5. Using Standard String Functions with Pointers
C provides many helpful string functions in <string.h>. They all operate
on char* pointers.
#include <stdio.h>
#include <string.h>
int main() {
char name[30] = "Tamil";
char suffix[] = " Technicians";
printf("Length of name = %zu\\n", strlen(name));
strcat(name, suffix); // append suffix to name
printf("After strcat: %s\\n", name);
if (strcmp(name, "Tamil Technicians") == 0) {
printf("Names match!\\n");
}
return 0;
}
strlen, strcpy, strcat, strcmp)
take char* or const char* internally – they are pointer-based.
Common danger: buffer overflow
When using strcat and strcpy, make sure the destination array is large
enough to hold the final result. Otherwise you write beyond the array, corrupt memory and crash.
6. Passing Arrays to Functions – Pointer-Style Parameters
When you pass an array to a function, you are actually passing a pointer to its first element. These declarations are equivalent:
void process(int a[], int n);
void process(int *a, int n); // same meaning
Example: total of marks array
#include <stdio.h>
int totalMarks(int marks[], int n) {
int i, sum = 0;
for (i = 0; i < n; i++) {
sum += marks[i]; // marks behaves like int * internally
}
return sum;
}
int main() {
int marks[5] = {80, 76, 90, 88, 92};
int n = 5;
int total = totalMarks(marks, n); // array passed as pointer
printf("Total = %d\\n", total);
return 0;
}
Inside totalMarks, the parameter marks is basically a pointer. Any change to
marks[i] will change the caller's array.
7. Real Use Case: Updating Array Values from a Function
Consider a real technician-style scenario: you have a list of daily energy readings from a meter, and you want to apply a calibration factor to all of them.
#include <stdio.h>
void applyCalibration(float readings[], int n, float factor) {
int i;
for (i = 0; i < n; i++) {
readings[i] = readings[i] * factor;
}
}
int main() {
float energy[4] = {10.5f, 11.0f, 9.8f, 10.2f};
int i;
applyCalibration(energy, 4, 1.05f); // 5% calibration increase
for (i = 0; i < 4; i++) {
printf("%.2f ", energy[i]);
}
printf("\\n");
return 0;
}
Because arrays are passed as pointers, applyCalibration directly modifies the
original energy array.
8. Passing Strings to Functions – const char*
Functions often receive strings as char* or const char*.
For example, a simple logging function:
#include <stdio.h>
void logMessage(const char *label, const char *msg) {
printf("[%s] %s\\n", label, msg);
}
int main() {
logMessage("INFO", "Program started");
logMessage("WARN", "Low battery");
logMessage("ERROR", "Sensor not responding");
return 0;
}
labelandmsgare bothconst char*pointers.- We promise not to modify the strings (hence
const).
const char* for input-only string parameters is a good habit.
It prevents accidental modification and helps the compiler catch mistakes.
9. Output Parameters – Returning Multiple Results via Pointers
Functions in C can return only one value directly. To return more than one result, we often use pointers as output parameters.
Example: Find min & max in an array
#include <stdio.h>
void findMinMax(int a[], int n, int *minOut, int *maxOut) {
int i;
*minOut = a[0];
*maxOut = a[0];
for (i = 1; i < n; i++) {
if (a[i] < *minOut) {
*minOut = a[i];
}
if (a[i] > *maxOut) {
*maxOut = a[i];
}
}
}
int main() {
int readings[] = {15, 20, 10, 25, 18};
int n = 5;
int minVal, maxVal;
findMinMax(readings, n, &minVal, &maxVal);
printf("Min = %d, Max = %d\\n", minVal, maxVal);
return 0;
}
Here:
minOutandmaxOutare pointers pointing to variables inmain().*minOutand*maxOutmodify those variables (output parameters).
10. Preview: Pointers to Functions (Just a Taste)
Full “function pointers” is an advanced topic, but you should know they exist. Just like you can store the address of a variable in a pointer, you can store the address of a function in a function pointer.
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*op)(int, int); // pointer to function taking (int,int) and returning int
op = &add; // or simply: op = add;
int result = op(10, 20); // calls add(10, 20)
printf("Result = %d\\n", result);
return 0;
}
Function pointers are used in callback systems, menus, and state machines. For now, just remember: pointers are not only for data, but also for functions.
11. Summary Table – Pointers with Arrays, Strings & Functions
| Topic | Pointer View | Typical Use Case |
|---|---|---|
| 1D Arrays | a ~ &a[0], type int*, float* etc. |
Iterate over samples, modify elements, pass to functions. |
| Strings | char* pointing to null-terminated char array. |
Print messages, labels, logs, process text using <string.h>. |
| Arrays to functions | Parameter int a[] is same as int *a. |
Process sensor readings, marks, charts, etc., inside functions. |
| Output parameters | int *out parameters write back to caller. |
Return multiple results like min/max, quotient/remainder, stats. |
| Function pointers (preview) | Pointers to code, syntax like int (*fp)(int,int). |
Callbacks, menu handlers, state machines, plug-in style design. |
12. Learning Checklist – Are You Comfortable With These?
- I understand that an array name decays to a pointer to its first element in expressions.
- I can iterate through an array using index notation or pointer arithmetic.
- I know the difference between a modifiable
chararray and a string literal viachar*. - I can pass arrays and strings to functions and understand that they are pointer parameters inside.
- I can write a function that uses pointer parameters as outputs (e.g.,
findMinMax). - I have a basic idea that functions themselves can be pointed to by function pointers.
Suggested Featured Image Prompt
Use this prompt in your AI image generator for this blog post thumbnail on TamilTechnicians.com:
“Flat modern 16:9 illustration on a light background. On the left, a horizontal row of boxes labeled `int readings[5]` shows an array of numbers (like 10, 20, 30, 40, 50) with index labels 0, 1, 2, 3, 4. A pointer `p` is drawn as a small box holding an address, with an arrow pointing to the first element of the array. On the top right, a `char msg[] = “Tamil”;` string is shown as a sequence of characters T A M I L `\0`, with a `char *s` arrow pointing to the ‘T’. At the bottom right, a small function box labeled `void process(int a[], int n)` has an arrow from the array diagram into the function, showing how arrays are passed as pointers. A South Indian / Tamil technician character stands near the diagrams with a pointer stick, next to small labels ‘arrays ↔ pointers’, ‘char* strings’, and ‘pointer parameters’. At the top, bold title text: ‘Pointers with Arrays, Strings & Functions’ and smaller subtitle: ‘Real Use Cases in C | Tamil Technicians’. Clean vector style, minimal colors, educational, high quality.”
FAQ: Pointers with Arrays, Strings & Functions
1. When should I use pointers instead of array indexes?
For most beginner programs, array indexes are perfectly fine. Pointers become more useful when you:
- Write generic functions that work with any part of an array (pass pointer to middle).
- Work with strings and standard C string functions.
- Need efficient low-level control, like in embedded systems.
2. Why does changing an array inside a function change it in main()?
Because when you pass an array to a function, you are passing a pointer to its first
element. The function operates directly on the same memory as main().
There is no copy of the whole array.
3. Should I always use const char* for string parameters?
If your function only reads from the string and never writes into it,
using const char* is recommended. It makes your intent clear and prevents accidental modification.
4. How do I know if I am going out of bounds with pointer arithmetic?
You must manually ensure that your pointer never crosses the valid range:
for arrays, that means from a to a + n. A common safe pattern is
to use a loop like for (p = a; p < a + n; p++), where you know a + n is
the one-past-last address.


