C Programs

C Programming – Structures


Programs at a Glance

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.

  1. Swapping of Two Integers Using Call by Value and Call by Reference
  2. Demonstration of Pointer Arithmetic
  3. Display Array Elements in Reverse Order Using Pointer
  4. Sum of n Array Elements Using Pointer
  5. Palindrome Check Using Pointer
  6. Print n City Names Using Pointers and Strings

1. Swapping of Two Integers Using Call by Value and Call by Reference


Problem Statement

Write a C program to swap two integers using (i) Call by Value and (ii) Call by Reference.

Student-Friendly Algorithm
  1. Start the program.
  2. Read two integers x and y from the user.
  3. Call a function swapValue(x, y) that swaps only local copies (call by value) and prints the result inside the function.
  4. Back in main(), display x and y to show that they are not changed.
  5. Call a function swapReference(&x, &y) that swaps the actual variables using pointers (call by reference).
  6. Display x and y again to show that their values have been swapped.
  7. Stop the program.
C Program

#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;
}
    
Sample Output

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
    
Explanation

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().

2. Demonstration of Pointer Arithmetic


Problem Statement

Write a C program to demonstrate pointer arithmetic.

Student-Friendly Algorithm
  1. Start the program.
  2. Declare an integer variable and initialize it with a value.
  3. Declare an integer pointer and store the address of the integer variable in it.
  4. Print the value stored at the pointer and the address held by the pointer.
  5. Increment the pointer by 1 and print the new address.
  6. Observe that the address changes by the size of the data type (for int, typically 4 bytes).
  7. Stop the program.
C Program

#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;
}
    
Sample Output

Value = 10
Address = 0x7ffc12345678
After pointer increment: 0x7ffc1234567c
    
Explanation

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.

3. Display Array Elements in Reverse Order Using Pointer


Problem Statement

Write a C program to display array elements in reverse order using a pointer.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of elements n and then read n integers into an array.
  3. Declare a pointer and make it point to the last element of the array.
  4. Use a loop to print the value pointed to, then decrement the pointer to move to the previous element.
  5. Repeat until all elements have been printed in reverse order.
  6. Stop the program.
C Program

#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;
}
    
Sample Output

Enter number of elements:
5
Enter array elements:
10 20 30 40 50
Elements in reverse order:
50 40 30 20 10
    
Explanation

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.

4. Sum of n Array Elements Using Pointer


Problem Statement

Write a C program to find the sum of n elements of an array using a pointer.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of elements n and then read n integers into an array.
  3. Declare a pointer and point it to the first element of the array.
  4. Initialize sum = 0.
  5. Use a loop to add the value pointed to by the pointer to sum, then increment the pointer to move to the next element.
  6. After processing all elements, display the final sum.
  7. Stop the program.
C Program

#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;
}
    
Sample Output

Enter number of elements:
4
Enter array elements:
5 10 15 20
Sum = 50
    
Explanation

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.

5. Palindrome Check Using Pointer


Problem Statement

Write a C program to check whether a given string is palindrome using pointers.

Student-Friendly Algorithm
  1. Start the program.
  2. Read a string from the user.
  3. Set a pointer start to the first character of the string.
  4. Set another pointer end to the last character of the string (before the null terminator).
  5. While start < end, compare the characters pointed to by start and end.
  6. If any pair does not match, declare that the string is not a palindrome and stop.
  7. If all pairs match, declare that the string is a palindrome.
  8. Stop the program.
C Program

#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;
}
    
Sample Output

Enter a string:
level
Palindrome String
    
Explanation

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.

6. Print n City Names Using Pointers and Strings


Problem Statement

Write a C program to print n city names using pointers and strings.

Student-Friendly Algorithm
  1. Start the program.
  2. Read the number of cities n.
  3. Declare a two-dimensional character array to store up to n city names.
  4. Read each city name into a separate row of the array.
  5. For each city, assign a pointer to the corresponding row (string) and print the string through the pointer.
  6. Repeat until all city names are displayed.
  7. Stop the program.
C Program

#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;
}
    
Sample Output

Enter number of cities:
3
Enter city name:
Hyderabad
Enter city name:
Chennai
Enter city name:
Mumbai
City Names:
Hyderabad
Chennai
Mumbai
    
Explanation

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.