O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Pointers
1. What are pointers?
Ans: Pointers refer to the memory lactation of another variable. It is possible to access and display
the address of memory location of another variable using ‘&’ operator. Pointer variable stores the mem-
ory address of any type of variable. The pointer variable and normal variable should be of the same type.
Memory space required for pointers of any data type is the same, whereas it is different for variables
of different data types. This is due to the fact that pointers store address that is an unsigned integer.
2. What are the advantages of using pointers in C programming?
Ans: Following are the advantages of pointers in C programming.
1. The memory is accessed efficiently with the pointers. The pointer assigns the memory space and
also it releases.
2. Pointers are used to allocate memory dynamically.
3. They are used with data structures. They are useful for representing one-dimensional and multi-
dimensional arrays.
4. They are utilized in linked lists and call by reference functions.
5. They access the elements of any type of array irrespective if its subscript range.
6. They save the memory space.
7. Execution time with pointer is fast, because data is manipulated with the address, i.e., direct access
to memory location.
8. Pointers are used in file handling.
3. How is a pointer declared?
Ans: Declaration of pointer variable is done with * operator. The * operator is also called indirection
or dereferencing operator. It is to be placed before the variable and after the data type.
A few examples of declaration of pointers in C are as follows:
9
M09_ITL-ESL4791_02_SE_C09.indd 187 12/22/2012 5:04:01 PM
II-188 Programming Concepts
int *a;
char *b;
float *c;
4.  Explain the use of (*) indirection operator.
Ans: Pointer variables are declared as follows:
Example:
int *x;
float *f;
char *y;
The indirection operator (*) is also called the dereferencing operator. When a pointer is dereferenced,
the value at that address stored by the pointer is retrieved.
The indirection operator (*) is used in two distinct ways with pointers, declaration and dereference.
1. When a pointer is declared, a star (*) indicates that it is a pointer and not a normal variable.
2. When the pointer is dereferenced, the indirection operator indicates that the value at that memory
location stored in the pointer is to be accessed rather than the address itself. For example,
printf('%d',*x); will print the value of another variable stored in memory location x.
5. Write a program to display the value of a variable and its address using pointer.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
int v=10,*p;
clrscr();
p=&v;
printf("\n Address of v = %u",p);
printf("\n Value of v = %d",*p);
printf("\n Address of p = %u",&p);
}
OUTPUT:
Address of v = 4060
Value of v = 10
Address of p = 4062
Explanation: In the above program, v is an integer variable and its value is 10. The variable p is
declared as pointer variable. The statement p = &v assigns address of v to p, i.e., p is the pointer to
variable v. To access the address and value of v, pointer p can be used. The value of p is nothing
but address of variable v. To display the value stored at that location, *p is used. The pointer variables
also have an address and displayed using ‘&’ operator. The statement used is printf("\n Address
of p = %u", &p);.The above explanation is followed by Figure 9.1.
M09_ITL-ESL4791_02_SE_C09.indd 188 12/22/2012 5:04:01 PM

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required