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(" Address of v = %u",p);
printf(" Value of v = %d",*p);
printf(" 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(" 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
Pointers II-189
The value of v is
10 and it is stored at location 4060. Address of v 4060 is assigned to variable p.
Address of pointer variable p is 4062. Here, p is pointer type variable, which points to the variable
v. Hence, it must be declared as int v=10 and pointer variable as int *p;.
6. Prove that pointer of any data type requires two bytes.
Ans:  The float variable requires 4 bytes of memory for storage, but pointer to a float requires only
2 bytes, as it stores the address of a float variable, which is an unsigned integer.
Similarly, the integer variable requires 2 bytes of memory for storage, but integer pointer to a float
requires only 2 bytes, as it stores the address of a float variable that is an unsigned integer.
Run the following program and verify the memory requirement to integer and float pointer.
4061
4060
v
10
Figure 9.1 Variable Addresses and Value
void main()
{
float a;
float *p;
int b,*pa;
clrscr();
p=&a;
pa=&b;
printf(" size of a=%d size of p=%d",sizeof(a), sizeof(p));
printf(" Address of a=%u and with pointer address of a=%u",&a,
p);
printf(" size of b=%d size of pa=%d",sizeof(b), sizeof(pa));
getche();
}
OUTPUT:
size of a=4 size of p=2
Address of a=65486 and with pointer address of a=65486
size of b=2 size of pa=2
Explanation: Memory required for floating and integer numbers are 4 and 2 bytes, respectively.
However, memory required to pointers of any data type is 2 bytes.
7. Explain the effect of ++ and –– operators with pointer of all data type.
Ans: Consider the following pointer declaration.
int *x;
++ and −− operators increment the normal variables, but when applied to a pointer of any data type they
point to the immediate next and previous memory locations.
M09_ITL-ESL4791_02_SE_C09.indd 189 12/22/2012 5:04:02 PM
II-190 Programming Concepts
Hence, ++x will point to immediate next memory location and −−x will point to immediate previous
memory location of x.
8. Apply post-increment operator on pointers in the program and show the effects.
Ans:
void main()
{
int b,*pa;
clrscr();
pa=&b;
printf(" Address of b with pointer=%u", pa);
pa++;
pa++;
printf(" Address of b after incrementing pointer for two times
= %u", pa);
getche();
}
OUTPUT:
Address of b with pointer=65492
Address of b after incrementing pointer for two times = 65496
Explanatio n:  The integer pointer variable pa is post-incremented two times in the above program.
Address of pointer after two post-incrementations is 65496. Initial address of pointer pa is 65492.
Each integer pointer requires two bytes and hence after every incrementation of integer pointer,
address gets incremented by two.
9. Apply post-decrement operator on pointers and show the effects.
Ans:
void main()
{
int b,*pa;
clrscr();
pa=&b;
printf(" Address of b with pointer=%u", pa);
pa--;
pa--;
printf(" Address of b after decrementing pointer for two
times= %u", pa);
getche();
}
M09_ITL-ESL4791_02_SE_C09.indd 190 12/22/2012 5:04:02 PM
Pointers II-191
Explanation: Integer needs two bytes to store in the memory. Hence, after decrementing pointer, each
time address gets reduced by two. Address of b after decrementing pointer for two times = 65488.
10. What do you mean by wild pointer?
Ans:
1. The wild pointer does not point either to a valid object or to a notable NULL value.
2. When pointer points to an unallocated memory location or to data value whose memory is de-allocated,
such pointer are called ‘wild pointer’.
3. The wild pointer generates garbage memory location and pendent reference.
11. Which are the reasons for pointer to become a wild pointer?
Ans: The pointer becomes wild due to following reasons:
1. Pointer declared but not initialized.
2. Pointer alternation.
3. Accessing destroyed data.
12. Write a program to show wild pointer and its output.
Ans:
OUTPUT:
Address of b with pointer = 65492
Address of b after decrementing pointer for two times = 65488
#include <stdio.h>
#include <conio.h>
void main()
{
int k,*x;
clrscr();
for(k=0;k<=3;k++)
printf("%u",x[k]);
}
OUTPUT:
7272 24330 30559 27753
Explanation: In this program, pointer x is not initialized. The successive locations are displayed.
13. Write a short note on constant pointer.
Ans: The address of the constant pointer cannot be modified.
char * const str = "Constant";
In the above example, it is not possible to modify the address of the pointer str. The following opera-
tions will generate error messages.
M09_ITL-ESL4791_02_SE_C09.indd 191 12/22/2012 5:04:02 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset