0% found this document useful (0 votes)
6 views7 pages

Pointer Programming Examples in C

The document provides a series of basic and intermediate pointer programs in C, covering topics such as pointer declaration, arithmetic, null pointers, void pointers, and dynamic memory allocation. Each section includes code examples that demonstrate the concepts, such as accessing array elements with pointers and using function pointers. The document serves as a practical guide for understanding and implementing pointer operations in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Pointer Programming Examples in C

The document provides a series of basic and intermediate pointer programs in C, covering topics such as pointer declaration, arithmetic, null pointers, void pointers, and dynamic memory allocation. Each section includes code examples that demonstrate the concepts, such as accessing array elements with pointers and using function pointers. The document serves as a practical guide for understanding and implementing pointer operations in C programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Basic Pointer Programs.

HU22CSEN0100302
P Saikrishna
1. Pointer Declaration and Initialization:
o Write a program to declare a pointer, assign the address of a variable, and
print the value using both the pointer and the variable.
#include <stdio.h>

int main() {
int value = 10;
int *ptr;
ptr = &value;

printf("Value of the variable: %d\n", value);


printf("Value using the pointer: %d\n", *ptr);

return 0;
}
2. Pointer Arithmetic:
o Demonstrate pointer increment, decrement, addition, and subtraction with
an array.
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
printf("Initial pointer value: %p\n", (void *)ptr);
printf("Value at initial position: %d\n", *ptr);
ptr++;
printf("After incrementing pointer: %p\n", (void *)ptr);
printf("Value after increment: %d\n", *ptr);
ptr--;
printf("After decrementing pointer: %p\n", (void *)ptr);
printf("Value after decrement: %d\n", *ptr);
ptr = ptr + 2;
printf("After adding 2 to pointer: %p\n", (void *)ptr);
printf("Value after addition: %d\n", *ptr);

ptr = ptr - 1;
printf("After subtracting 1 from pointer: %p\n", (void *)ptr);
printf("Value after subtraction: %d\n", *ptr);

return 0;
}

3. Null Pointer:
o Write a program to demonstrate a null pointer and check if a pointer is null.
#include <stdio.h>

int main() {
int *ptr = NULL;

if (ptr == NULL) {
printf("The pointer is a null pointer.\n");
} else {
printf("The pointer is not null.\n");
}

return 0;
}

4. Void Pointer:
o Implement a program to use a void pointer and typecast it to access data.
#include <stdio.h>

int main() {
int num = 10;
float pi = 3.14;
void *vptr;

vptr = &num;
printf("Integer value: %d\n", *(int *)vptr);

vptr = &pi;
printf("Float value: %f\n", *(float *)vptr);

return 0;
}
Intermediate Pointer Programs
5. Array and Pointer Relationship:
o Write a program to access array elements using pointers.
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;
int i;

for (i = 0; i < 5; i++) {


printf("Element %d: %d\n", i, *(ptr + i));
}

return 0;
}

6. Pointer to Pointer:
o Implement a program to demonstrate the use of a pointer to a pointer.
#include <stdio.h>

int main() {
int value = 10;
int *ptr;
int **pptr;

ptr = &value;
pptr = &ptr;

printf("Value of value: %d\n", value);


printf("Value using ptr: %d\n", *ptr);
printf("Value using pptr: %d\n", **pptr);

return 0;
}

7. Function Pointers:
o Create a program to use a function pointer to call a function.

#include <stdio.h>

void displayMessage() {
printf("Hello from a function pointer!\n");
}

int main() {
void (*func_ptr)();
func_ptr = &displayMessage;
(*func_ptr)();

return 0;
}

8. Dynamic Memory Allocation:


o Write a program to allocate and deallocate memory dynamically using malloc,
calloc, realloc, and free.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr1 = (int *)malloc(sizeof(int));
if (ptr1 == NULL) {
return 1;
}
*ptr1 = 10;
printf("Value allocated with malloc: %d\n", *ptr1);

int *ptr2 = (int *)calloc(5, sizeof(int));


if (ptr2 == NULL) {
return 1;
}
printf("Values allocated with calloc (initialized to 0): ");
for (int i = 0; i < 5; i++) {
printf("%d ", ptr2[i]);
}
printf("\n");

ptr2 = (int *)realloc(ptr2, 10 * sizeof(int));


if (ptr2 == NULL) {
return 1;
}
printf("After reallocating, size is 10 integers.\n");

free(ptr1);
free(ptr2);

return 0;
}
9. Passing Pointers to Functions:
o Demonstrate passing pointers to a function to modify the value of a variable.
#include <stdio.h>
void modifyValue(int *p) {
*p = 50;
}
int main() {
int num = 10;
printf("Before function call: %d\n", num);
modifyValue(&num);
printf("After function call: %d\n", num);

return 0;
}

10. String Manipulation Using Pointers:


o Implement string operations (e.g., length, copy, concatenate) using pointers.
#include <stdio.h>

#include <string.h>

int main() {

char str1[] = "Hello";

char str2[20];

char str3[] = " World";

int len = 0;

char *p = str1;

while (*p != '\0') {

len++;

p++;

printf("Length of string 'Hello': %d\n", len);


char *src = str1;

char *dest = str2;

while ((*dest++ = *src++) != '\0');

printf("Copied string: %s\n", str2);

char *s1 = str1;

char *s2 = str3;

while (*s1 != '\0') {

s1++;

while ((*s1++ = *s2++) != '\0');

printf("Concatenated string: %s\n", str1);

return 0;

You might also like