Define pointer.
Explain pointer variable declaration and initialization with suitable
example.
A pointer is a variable that stores the address of another variable.
Pointer Declaration
When we declare a pointer, we use the * symbol.
int *p;
Pointer Initialization
When we declare a pointer, we use the & (address-of) operator symbol.
int num = 10;
int *p;
p = #
Example Program:
#include<stdio.h>
int main() {
int num = 10;
int *p;
p = #
printf("Value of num: %d", num);
printf("Address of num: %p", &num);
printf("Value of ptr: %p", p);
printf("Value pointed by ptr: %d", *p);
return 0;
Output :
Value of num: 10
Address of num: < memory address>
Value of ptr: < memory address of num>
Value pointed by ptr: 10
Applications of Pointers
1. To store the address of a variable.
2. To access and traverse arrays efficiently.
3. To manipulate strings.
4. To implement data structures.
5. To use dynamic memory allocation.
Define STRING. Discuss the various string handling/manipulation functions in C.
A string is a sequence of characters stored in contiguous memory locations.
1. strlen(str)
• Purpose: Returns the length of the string.
Example:
char str[] = "Hello";
printf("%d", strlen(str)); // Output: 5
2. strcpy(dest, src)
• Purpose: Copies the string source into destination.
Example:
char src[] = "Hello";
char dest[10];
strcpy(dest, src);
printf("%s", dest); // Output: Hello
3. strchr(str, ch)
• Purpose: Finding a character in a string.
Example:
char str[] = "Hello";
printf("%s", strchr(str, 'l')); // Output: llo
4. strcat(dest, src)
• Purpose: Concatenates(Joins) the string source to the end of dest.
Example:
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1); // Output: Hello World
5. strcmp(str1, str2)
• Purpose: Compares two strings.
Example:
char str1[] = "Apple";
char str2[] = "Orange";
printf("%d", strcmp(str1, str2)); // Output: negative value
Write a c program using pointers to compute mean, sum, std deviation of all elements
stored in an array
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n], sum = 0, mean, stddev = 0;
int *ptr = arr;
printf("Enter %d elements:", n);
for(int i = 0; i < n; i++)
scanf("%d", ptr + i);
// Calculate sum
for(int i = 0; i < n; i++)
sum += *(ptr + i);
// Calculate mean
mean = sum / n;
// Calculate standard deviation
for(int i = 0; i < n; i++)
stddev += pow(*(ptr + i) - mean, 2);
stddev = sqrt(stddev / n);
printf("Sum = %d", sum);
printf("Mean = %d", mean);
printf("Standard Deviation = %d", stddev);
return 0;
Output :
Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50
Sum = 150
Mean = 30
Standard Deviation = 14.14