These worked examples correspond to the pointer exercises on the Pointers in C tutorial. Each solution is complete and can be compiled independently.
#include <stdio.h>
int main(void) {
int number = 10;
float value = 2.5f;
printf("int address : %p\n", (void *)&number);
printf("float address : %p\n", (void *)&value);
return 0;
}
#include <stdio.h>
int main(void) {
long value = 2147483647L;
long *ptr = &value;
printf("value = %ld\n", *ptr);
printf("address = %p\n", (void *)ptr);
return 0;
}
#include <stdio.h>
int main(void) {
char text[] = "plus2net";
char *ptr = text;
while (*ptr != '\0') {
printf("address = %p, value = %c\n",
(void *)ptr, *ptr);
ptr++;
}
return 0;
}
#include <stdio.h>
int main(void) {
int first = 100;
int second = 200;
int *p1 = &first;
int *p2 = &second;
printf("sum = %d\n", *p1 + *p2);
return 0;
}
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int first;
int second;
printf("Enter two integers: ");
if (scanf("%d %d", &first, &second) != 2) {
printf("Invalid input.\n");
return 1;
}
swap(&first, &second);
printf("first = %d, second = %d\n", first, second);
return 0;
}
#include <stdio.h>
int highest(const int *a, const int *b) {
return (*a > *b) ? *a : *b;
}
int main(void) {
int first = 10;
int second = 15;
printf("Highest = %d\n", highest(&first, &second));
return 0;
}
#include <stdio.h>
void display_array(const int *values, size_t count) {
for (size_t i = 0; i < count; i++) {
printf("Element: %d\n", values[i]);
}
}
int main(void) {
int values[] = {1, 2, 3, 4, 5};
size_t count = sizeof values / sizeof values[0];
display_array(values, count);
return 0;
}
#include <stdio.h>
void add_two(int *values, size_t count) {
for (size_t i = 0; i < count; i++) {
values[i] += 2;
}
}
int main(void) {
int values[] = {1, 2, 3, 4, 5};
size_t count = sizeof values / sizeof values[0];
add_two(values, count);
for (size_t i = 0; i < count; i++) {
printf("%d ", values[i]);
}
putchar('\n');
return 0;
}
#include <stdio.h>
void min_max(
const int *values,
size_t count,
int *minimum,
int *maximum
) {
*minimum = values[0];
*maximum = values[0];
for (size_t i = 1; i < count; i++) {
if (values[i] < *minimum) {
*minimum = values[i];
}
if (values[i] > *maximum) {
*maximum = values[i];
}
}
}
int main(void) {
int values[] = {-8, 12, 40, 3, 17};
size_t count = sizeof values / sizeof values[0];
int minimum;
int maximum;
min_max(values, count, &minimum, &maximum);
printf("Highest = %d, Lowest = %d\n", maximum, minimum);
return 0;
}
#include <stdio.h>
void sort_ascending(int *values, size_t count) {
for (size_t i = 0; i < count; i++) {
for (size_t j = i + 1; j < count; j++) {
int *left = values + i;
int *right = values + j;
if (*left > *right) {
int temp = *left;
*left = *right;
*right = temp;
}
}
}
}
int main(void) {
int values[] = {-34, 14, -11, 15, 0, -20};
size_t count = sizeof values / sizeof values[0];
sort_ascending(values, count);
for (size_t i = 0; i < count; i++) {
printf("%d\n", values[i]);
}
return 0;
}
Output
-34
-20
-11
0
14
15
#include <stdio.h>
#include <string.h>
int main(void) {
const char text[] = "welcome to plus2net";
const char *ptr = text;
size_t length = strlen(text);
for (size_t i = length; i > 0; i--) {
putchar(*(ptr + i - 1));
}
putchar('\n');
return 0;
}
#include <stdio.h>
#include <string.h>
int main(void) {
const char text[] = "plus2net";
const char *ptr = text;
size_t length = strlen(text);
if (length == 0) {
printf("String is empty.\n");
return 0;
}
printf("Middle character: %c\n", *(ptr + length / 2));
return 0;
}
For an even-length string, integer division selects the character immediately to the right of the halfway boundary, matching the original exercise requirement.
#include <stdio.h>
int contains(
const int *values,
size_t count,
int target
) {
for (size_t i = 0; i < count; i++) {
if (values[i] == target) {
return 1;
}
}
return 0;
}
int main(void) {
int values[] = {5, 12, 18, 21, 30};
size_t count = sizeof values / sizeof values[0];
printf("%s\n",
contains(values, count, 18)
? "Value found"
: "Value not found");
return 0;
}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.