Finding Minimum and Maximum Values Using Pointers
Version 1: Passing Pointers for Min/Max Output
This approach uses output parameters through pointers to return both the minimum and maximum values from an array.
#include <stdio.h>
#define SIZE 5
void read_values(int arr[], int count);
void display_values(int arr[], int count);
void locate_extremes(int arr[], int count, int* min_val, int* max_val);
int main() {
int numbers[SIZE];
int minimum, maximum;
printf("Enter %d integers:\n", SIZE);
read_values(numbers, SIZE);
printf("Original values:\n");
display_values(numbers, SIZE);
locate_extremes(numbers, SIZE, &minimum, &maximum);
printf("Results:\n");
printf("minimum = %d, maximum = %d\n", minimum, maximum);
return 0;
}
void read_values(int arr[], int count) {
for (int i = 0; i < count; i++)
scanf_s("%d", &arr[i]);
}
void display_values(int arr[], int count) {
for (int i = 0; i < count; i++)
printf("%d ", arr[i]);
printf("\n");
}
void locate_extremes(int arr[], int count, int* min_val, int* max_val) {
*min_val = *max_val = arr[0];
for (int i = 0; i < count; i++) {
if (arr[i] < *min_val)
*min_val = arr[i];
else if (arr[i] > *max_val)
*max_val = arr[i];
}
}
The locate_extremes function traverses the array once while updating both extreme values through dereferenced pointers. The min_val and max_val pointers reference the minimum and maximum variables in main.
Version 2: Returning a Pointer to the Maximum Element
This variant returns a pointer directly to the array element containing the maximum value.
#include <stdio.h>
#define SIZE 5
void read_values(int arr[], int count);
void display_values(int arr[], int count);
int* find_largest(int arr[], int count);
int main() {
int numbers[SIZE];
int* max_ptr;
printf("Enter %d integers:\n", SIZE);
read_values(numbers, SIZE);
printf("Original values:\n");
display_values(numbers, SIZE);
max_ptr = find_largest(numbers, SIZE);
printf("Maximum value: %d\n", *max_ptr);
return 0;
}
void read_values(int arr[], int count) {
for (int i = 0; i < count; i++)
scanf_s("%d", &arr[i]);
}
void display_values(int arr[], int count) {
for (int i = 0; i < count; i++)
printf("%d ", arr[i]);
printf("\n");
}
int* find_largest(int arr[], int count) {
int idx = 0;
for (int i = 1; i < count; i++)
if (arr[i] > arr[idx])
idx = i;
return &arr[idx];
}
The find_largest function tracks the index of the largest element and returns its address within the original array.
Two-Dimensional Arrays and Pointer Types
Different Access Patterns for 2D Arrays
#include <stdio.h>
int main() {
int matrix[2][4] = { {1, 9, 8, 4}, {2, 0, 4, 9} };
int row, col;
int* element_ptr;
int (*row_ptr)[4];
printf("Method 1: Direct array subscripting\n");
for (row = 0; row < 2; row++) {
for (col = 0; col < 4; col++)
printf("%d ", matrix[row][col]);
printf("\n");
}
printf("\nMethod 2: Element pointer traversal\n");
for (element_ptr = &matrix[0][0], row = 0; element_ptr < &matrix[0][0] + 8; element_ptr++, row++) {
printf("%d ", *element_ptr);
if ((row + 1) % 4 == 0)
printf("\n");
}
printf("\nMethod 3: Pointer to array traversal\n");
for (row_ptr = matrix; row_ptr < matrix + 2; row_ptr++) {
for (col = 0; col < 4; col++)
printf("%d ", *(*row_ptr + col));
printf("\n");
}
return 0;
}
Pointer Declaration Syntax Distinction
int (*ptr)[4]; // ptr is a pointer to an array of 4 integers
int *ptr[4]; // ptr is an array of 4 pointers to integers
The parentheses in int (*ptr)[4] bind the asterisk to ptr first, making it a pointer to an array. Without parentheses, int *ptr[4] creates an array of pointers due to higher precedence of the brackets.
Character Replacement in Strings
#include <stdio.h>
#define MAX_LEN 80
void substitute_char(char* text, char target, char replacement);
int main() {
char sentence[MAX_LEN] = "Programming is difficult or not, it is a question.";
printf("Original text:\n%s\n", sentence);
substitute_char(sentence, 'i', '*');
printf("Modified text:\n%s\n", sentence);
return 0;
}
void substitute_char(char* text, char target, char replacement) {
while (*text) {
if (*text == target)
*text = replacement;
text++;
}
}
The substitute_char function iterates through the string, replacing each occurrence of target with replacement until the null terminator is reached.
String Truncation at First Character Match
#include <stdio.h>
#define MAX_LEN 80
char* cut_at(char* text, char delimiter);
int main() {
char buffer[MAX_LEN];
char delim;
while (printf("Enter string: "), gets(buffer) != NULL) {
printf("Enter delimiter character: ");
delim = getchar();
printf("Truncating...\n");
cut_at(buffer, delim);
printf("Result: %s\n\n", buffer);
getchar();
}
return 0;
}
char* cut_at(char* text, char delimiter) {
int idx = 0;
while (text[idx] != '\0') {
if (text[idx] == delimiter) {
text[idx] = '\0';
break;
}
idx++;
}
return text;
}
The extra getchar() call consumes the newline character remaining in the input buffer after reading the delimiter, ensuring subsequent input operations read correctly.
Chinese ID Number Validation
#include <stdio.h>
#include <string.h>
#define COUNT 5
int validate_id(char* id_str);
int main() {
char* id_list[COUNT] = {
"31010120000721656X",
"3301061996X0203301",
"53010220051126571",
"510104199211197977",
"53010220051126133Y"
};
for (int i = 0; i < COUNT; i++)
printf("%s\t%s\n", id_list[i], validate_id(id_list[i]) ? "Valid" : "Invalid");
return 0;
}
int validate_id(char* id_str) {
int len = 0;
while (id_str[len] != '\0')
len++;
if (len != 18)
return 0;
for (int i = 0; i < 17; i++)
if (id_str[i] < '0' || id_str[i] > '9')
return 0;
if ((id_str[17] < '0' || id_str[17] > '9') && id_str[17] != 'X')
return 0;
return 1;
}
The validation checks that the ID has exactly 18 characters, the first 17 positions contain only digits, and the final character is either a digit or the letter 'X'.
Caesar Cipher Implemantation
#include <stdio.h>
#define MAX_LEN 80
void encrypt(char* text, int shift);
void decrypt(char* text, int shift);
int main() {
char message[MAX_LEN];
int offset;
printf("Enter text: ");
gets(message);
printf("Enter shift value: ");
scanf_s("%d", &offset);
encrypt(message, offset);
printf("Encrypted: %s\n", message);
decrypt(message, offset);
printf("Decrypted: %s\n", message);
return 0;
}
void encrypt(char* text, int shift) {
for (int i = 0; text[i] != '\0'; i++) {
if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = text[i] + shift;
if (text[i] > 'z')
text[i] = text[i] - 26;
}
else if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = text[i] + shift;
if (text[i] > 'Z')
text[i] = text[i] - 26;
}
}
}
void decrypt(char* text, int shift) {
for (int i = 0; text[i] != '\0'; i++) {
if (text[i] >= 'a' && text[i] <= 'z') {
text[i] = text[i] - shift;
if (text[i] < 'a')
text[i] = text[i] + 26;
}
else if (text[i] >= 'A' && text[i] <= 'Z') {
text[i] = text[i] - shift;
if (text[i] < 'A')
text[i] = text[i] + 26;
}
}
}
The encryption shifts alphabetic characters forward by the specified amount, wrapping around at the alphabet boundaries. Decryption reverses this process by shifting backward.
Sorting Command Line Arguments
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void sort_strings(int count, char* strings[]);
int main(int argc, char* argv[]) {
sort_strings(argc - 1, argv + 1);
for (int i = 1; i < argc; i++)
printf("Hello, %s\n", argv[i]);
return 0;
}
void sort_strings(int count, char* strings[]) {
char* temp;
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - 1 - i; j++) {
if (strcmp(strings[j], strings[j + 1]) > 0) {
temp = strings[j];
strings[j] = strings[j + 1];
strings[j + 1] = temp;
}
}
}
}
This program accepts command line arguments, sorts them alphabetically using a bubble sort algorithm with strcmp, and displays a greeting for each sorted argument. The argv + 1 skips the program name, and argc - 1 represents the actual number of arguments to sort.