You are on page 1of 2

Experiment-8 : Pointers

EXPERIMENT-8A

Aim: To demonstrate the pointer declaration and its use.

Pointers :
- A pointer is a variable that stores the address of another variable. Unlike other variables
that hold values of a certain type, pointer holds the address of a variable. For example,
an integer variable holds (or you can say stores) an integer value, however an integer
pointer holds the address of a integer variable.
- Pointers are symbolic representation of addresses.

Syntax:
datatype *var_name;

int *ptr; //ptr can point to an address which holds int data.

Pointer initialization is the process of assigning address of a variable to a pointer variable. It


contains the address of a variable of the same data type. In C language address operator ‘&’ is
used to determine the address of a variable. The ‘&’ (immediately preceding a variable name)
returns the address of the variable associated with it.

//C program to declare & use of pointer variable


#include <stdio.h>
int main()
{
int b=50; //variable declaration
int *p; //pointer variable declaration
p = &b; //store address of variable b in pointer p

printf("Address of variable b stored in pointer is %u\n", p);


printf("\nAccessing value of variable b using pointer is %u\n", *p);
return 0;
}

Output:

Address of variable b stored in pointer is 3272240676

Accessing value of variable b using pointer is 50

Conclusion: Thus, the pointer declaration and its use is studied.

Note : Draw a flowchart for the same program


EXPERIMENT-8B

Aim: To demonstrate the implementation of pointer on array.


Array of pointers
An array of pointers is an indexed set of variables, where the variables are pointers
(referencing a location in memory). An array of pointers is useful to allow us to
numerically index a large set of variables.

//C program to access array elements using pointer.


#include <stdio.h>
int main()
{
int i;
int num[] = {24, 34, 12, 44, 56, 17}; // declare & initialize array
int *pnum; //declare pointer
pnum = num; //assigning address of array to pointer

printf("Accessing values of array num using pointer pnum \n");


for (i=0; i < 6; i++)
{
printf("%d\n", *pnum);
pnum++; //incrementing pointer to next location
}
return 0;
}

Output :
Accessing values of Array num using pointer pnum.
24
34
12
44
56
17

Conclusion: Thus, implementation of pointer on array is studied.

Flowchart: Flowchart for above program

You might also like