Programming in C - Pointers
Programming in C - Pointers
Module III
Pointers:
● A pointer is a derived data type in C.
● A pointer is a variable that stores the memory address of another variable.
● Instead of holding a data value directly, a pointer holds the location where the data is stored.
This allows for efficient manipulation and access of data in the memory.
● Pointer Declaration:
○ Pointers are declared with the datatype of the data it points to.
○ datatype *pointername;
○ Eg: int *ptr;
○ The ‘*’ symbol can appear anywhere between the datatype and the pointer name.
Hence the following declarations are also valid.
int* ptr;
int * ptr;
○ Here ptr is a pointer variable which can be used to point to an integer variable.
○ The symbol * is used to denote a pointer variable. Its meaning is ‘value at address’.
○ This operator ‘*’ is called the indirection operator or dereferencing operator.
○ Assigning the address of a normal variable to a pointer variable can be done by the &
symbol. Here the meaning of the symbol ‘&’ is ‘address of’.
○ Eg: int var=50;
ptr = &var;
● Say, the address of the variable var is 1001. By the statement ptr=&var; this address is
stored at the pointer ptr. Thus the variable var can be now accessed with the pointer as
*ptr. That means, if the value *ptr is changed, the value of var will be changed,
because both denote the same value.
● Pointer Initialization:
○ The pointer can be assigned with the address of a variable when it is declared. This is
called pointer initialization.
○ Eg: int var = 10;
int *ptr = &var; // pointer initialization
● We can assign many pointers to the same variable, provided all are of the same datatype.
○ Eg: int x;
int *p1=&x, *p2=&x, *p3=&x;
● Also we can use a pointer to point to many variables, one at a time, provided all are of the
same type.
○ Eg: int x, y, z, *p;
…
p = &x;
…
p = &y;
…
p = &z;
free(a);
}
Array of Pointers
● The main use of an array of pointers is the handling of a table of strings such as all students
in a class.
● The normal initialization of a 2-dimensional character array for such a purpose is given below.
char name[3][10] = {“New Zealand”, Australia”, “India” };
● This can be done by array of pointers as
char *name[3] = {“New Zealand”, Australia”, “India” };
○ Here name is an array of three pointers where each pointer points to a particular string.
● The character arrays with the rows of varying length are called ragged arrays and are better
handled by pointers.
********************************************
Nov 2022
1. ...........is a variable that can hold the memory address of another variable. (1)
2. Write the syntax of malloc() (1)
3. What is Call by Reference? Give an example. (3)
4. Write advantages of dynamic memory allocation. (3)
5. How to access the elements of an array using pointers? (3)
6. How to initialize a string using pointers? (3)
7. Write a C program to sort a single dimensional array of size ‘10’ in ascending order using
pointer to array. (7)
8. Explain the use of pointers to arrays with an example. (7)
Nov 2023
1. ...........function used to dynamically deallocate a memory. (1)
2. Write the syntax of declaring a pointer variable. (1)
3. Illustrate the method of passing pointers to a function with an example. (3)
4. Define pointer. What are the advantages of pointers in C? (3)
5. Write the output of the following code. (3)
#include<stdio.h>
int main(){
int a[5]={1,2,3,4,5};
int *p;
p=a;
for(int i=0;i<5;i++) {
printf("\n%x",*p);
p++; }
return 0; }
6. Write a short note on array of pointers. (3)
7. Explain any two pointer arithmetic operations with examples. (3)
8. Compare malloc() and calloc(). (4)
9. Write a C Program to find the sum of two matrices using pointers. (7)