0% found this document useful (0 votes)
13 views

Assignment On Pointers

This document contains 4 examples demonstrating the use of pointers in C programming: 1) a pointer to a single integer variable, 2) a pointer to an integer array, 3) pointers to strings in an array of character pointers, and 4) passing a pointer to a function to modify the pointed variable by reference. Each example includes the relevant code, outputs the address and/or value of the variables and pointers, and demonstrates how pointers allow access and modification of variable values.

Uploaded by

harshadgore742
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment On Pointers

This document contains 4 examples demonstrating the use of pointers in C programming: 1) a pointer to a single integer variable, 2) a pointer to an integer array, 3) pointers to strings in an array of character pointers, and 4) passing a pointer to a function to modify the pointed variable by reference. Each example includes the relevant code, outputs the address and/or value of the variables and pointers, and demonstrates how pointers allow access and modification of variable values.

Uploaded by

harshadgore742
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment on Pointers

1)Pointers to a single integer variable


#include <stdio.h>

int main() {

int num = 10;

int *ptr = &num;

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

You might also like