Assignment on Pointers
1)Pointers to a single integer variable
#include <stdio.h>
int main() {
int num = 10;
int *ptr = #
printf("Value of num: %d\n", num);
printf("Address of num: %p\n", &num);
printf("Value of ptr: %p\n", ptr);
printf("Value of *ptr: %d\n", *ptr);
return 0;
Output:-
Value of num: 10
Address of num: 0x7ffc46b32da4
Value of ptr: 0x7ffc46b32da4
Value of *ptr: 10
2) Pointers to array:-
#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
int (*ptr)[3] = &arr;
printf("Value of arr[0]: %d\n", arr[0]);
printf("Address of arr[0]: %p\n", &arr[0]);
printf("Value of ptr: %p\n", ptr);
printf("Value of *ptr[0]: %d\n", (*ptr)[0]);
return 0;
}
Output;-
Value of arr[0]: 1
Address of arr[0]: 0x7ffdd748af3c
Value of ptr: 0x7ffdd748af3c
Value of *ptr[0]: 1
3)Pointers to string;-
#include <stdio.h>
#include <stdlib.h>
int main(void){
char *a[5] = {"Hi", "I", "am", "Harsh"}; // array of pointer a having size 5 and data type as char is
created.
int i;
printf ( "The locations of the strings are:");
for (i=0; i<5; i++)
printf ("%d", a[i]);
return 0;
}
Output:-
The locations of the strings are:42025044202507420250942025120
4)Pointers as function parameters(call by reference)
#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);
printf("After function call x=%d \n", x);
return 0;
}
Output:-
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200