This section lists the programs covered under Pointers and Parameter Passing in C. Click on a program title to jump directly to its detailed explanation.
Write a C program to swap two integers using (i) Call by Value and (ii) Call by Reference.
x and y from the user.swapValue(x, y) that swaps only local copies (call by value) and prints the result inside the function.main(), display x and y to show that they are not changed.swapReference(&x, &y) that swaps the actual variables using pointers (call by reference).x and y again to show that their values have been swapped.
#include <stdio.h>
void swapValue(int a, int b) {
int temp = a;
a = b;
b = temp;
printf("Inside Call by Value: a = %d, b = %d\n", a, b);
}
void swapReference(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x, y;
printf("Enter two numbers:\n");
scanf("%d %d", &x, &y);
swapValue(x, y);
printf("After Call by Value: x = %d, y = %d\n", x, y);
swapReference(&x, &y);
printf("After Call by Reference: x = %d, y = %d\n", x, y);
return 0;
}
Enter two numbers:
10 20
Inside Call by Value: a = 20, b = 10
After Call by Value: x = 10, y = 20
After Call by Reference: x = 20, y = 10
In call by value, the function swapValue() receives copies of x and y, so any changes affect only the local parameters a and b. After the function returns, the original variables x and y in main() remain unchanged.
In call by reference, the function swapReference() receives addresses of x and y, and uses pointers *a and *b to modify their actual memory locations. As a result, the values of x and y are really swapped in main().
Write a C program to demonstrate pointer arithmetic.
int, typically 4 bytes).
#include <stdio.h>
int main() {
int a = 10;
int *p = &a;
printf("Value = %d\n", *p);
printf("Address = %p\n", (void *)p);
p++;
printf("After pointer increment: %p\n", (void *)p);
return 0;
}
Value = 10
Address = 0x7ffc12345678
After pointer increment: 0x7ffc1234567c
A pointer stores a memory address, and printing it with %p shows this address in a machine-dependent format. When an int * pointer is incremented (p++), it moves to the next integer location in memory, increasing the address by sizeof(int) bytes.
This behavior is called pointer arithmetic and depends on the data type pointed to: for example, a float * would move by sizeof(float) bytes when incremented. The program helps visualize how pointers step through memory.
Write a C program to display array elements in reverse order using a pointer.
n and then read n integers into an array.
#include <stdio.h>
int main() {
int a[50], n, i;
int *p;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
p = &a[n - 1];
printf("Elements in reverse order:\n");
for (i = 0; i < n; i++) {
printf("%d ", *p);
p--;
}
return 0;
}
Enter number of elements:
5
Enter array elements:
10 20 30 40 50
Elements in reverse order:
50 40 30 20 10
The pointer p is initialized to the address of the last array element &a[n - 1]. Dereferencing *p prints the value at the current position, and then p-- moves the pointer one element backwards in memory.
Repeating this process n times visits all elements from last to first without changing the original array. This example shows how pointers can be used to traverse arrays in different directions.
Write a C program to find the sum of n elements of an array using a pointer.
n and then read n integers into an array.sum = 0.sum, then increment the pointer to move to the next element.
#include <stdio.h>
int main() {
int a[50], n, i, sum = 0;
int *p;
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
p = a; // Same as &a[0]
for (i = 0; i < n; i++) {
sum += *p;
p++;
}
printf("Sum = %d\n", sum);
return 0;
}
Enter number of elements:
4
Enter array elements:
5 10 15 20
Sum = 50
The pointer p initially points to the first element of the array, and *p gives the current element value. In each iteration, this value is added to sum, and then p++ moves the pointer to the next element in memory.
This method avoids using array indexing inside the loop and instead relies on pointer arithmetic, reinforcing the idea that arrays and pointers are closely related in C.
Write a C program to check whether a given string is palindrome using pointers.
start to the first character of the string.end to the last character of the string (before the null terminator).start < end, compare the characters pointed to by start and end.
#include <stdio.h>
#include <string.h>
int main() {
char s[50];
char *start, *end;
printf("Enter a string:\n");
scanf("%49s", s);
start = s;
end = s + strlen(s) - 1;
while (start < end) {
if (*start != *end) {
printf("Not a Palindrome\n");
return 0;
}
start++;
end--;
}
printf("Palindrome String\n");
return 0;
}
Enter a string:
level
Palindrome String
The pointer start moves forward from the beginning of the string, while end moves backward from the end. At each step, the characters *start and *end are compared; if any pair differs, the string cannot be a palindrome.
When the two pointers cross or meet without a mismatch, it means the string reads the same forwards and backwards. Using pointers in this way avoids creating a separate reversed copy of the string.
Write a C program to print n city names using pointers and strings.
n.n city names.
#include <stdio.h>
int main() {
char cities[10][30];
char *p;
int n, i;
printf("Enter number of cities:\n");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter city name:\n");
scanf("%29s", cities[i]);
}
printf("City Names:\n");
for (i = 0; i < n; i++) {
p = cities[i];
printf("%s\n", p);
}
return 0;
}
Enter number of cities:
3
Enter city name:
Hyderabad
Enter city name:
Chennai
Enter city name:
Mumbai
City Names:
Hyderabad
Chennai
Mumbai
The two-dimensional array cities stores multiple strings, one per row, where each row is a character array for a single city name. For each row, the pointer p is assigned the base address of that row, so p behaves like a normal string pointer.
Printing %s with p displays the entire city name stored in that row. This example illustrates how pointers can be used together with arrays of strings to access and display text data efficiently.