Pointers II-197
Here, in this declaration, 20 bytes are allocated to pointer variable pnt of type int and base
address is returned to pointer pnt.
2. free() function: It is used to release the memory allocated by memory allocating functions.
Thus, by using this function, the wastage of memory is prevented. The declaration of the function
is as follows:
free (pnt)
In the above declaration, pnt is a pointer. The free() function releases the memory occupied by
the pointer variable pnt.
30.  Explain the concept of dynamic memory allocation.
Ans: There is a technique by which a program can obtain space in the main memory at run time that
is called dynamic allocation. Dynamic allocation is a technique in which a program can acquire storage
space in the main memory. In this method, the space for the program is allocated from the free space
during the execution of the program. The free region of the memory is called the heap. Depending upon
the memory model, the portions of the heap are allocated for memory requirement. Conceptually, the
C programs are stored in the free space from the heap. The amount of memory requirement is decided
by how the program is designed. For example, if a program is developed with many recursive functions,
then this is implemented with stack.
31. Describe the various functions used for memory allocation.
Ans: The various functions that are used for memory allocation are malloc, calloc, free,
realloc and coreleft.
For malloc and free functions, refer the answer to the question 19.
1. calloc()function: It is useful for allocating multiple blocks of memory. It is declared with
two arguments. The prototypes are declared in alloc.h and stdlib.h. The format of the
calloc() function is as follows:
pnt = (int *) calloc (4,2);
The above declaration allocates four blocks of memory; each block contains 2 bytes. The base
address is stored in the integer pointer. This function is usually used for allocating memory for
array and structure.
2. realloc()function: It reallocates main memory. The prototypes are declared in alloc.h and
stdlib.h. Attempts are made to shrink or enlarge the previously allocated memory by malloc() or
calloc() functions. It returns the address of the reallocated block, which can be different from
the original address. If the block cannot be reallocated or size == 0, realloc() returns NULL.
The syntax of the function is as follows:
ptr = (type *) realloc (ptr, new size);
3. coreleft function: It returns a measure of unused memory. If the memory model selected is tiny,
small or medium, then follow the function declaration as per statement (a). If the memory model
selected is compact, large or huge, follow the declaration (b).
(a) unsigned coreleft(void);
(b) unsigned long coreleft (void);
M09_ITL-ESL4791_02_SE_C09.indd 197 12/22/2012 5:04:02 PM
II-198 Programming Concepts
Multiple-choice Questions
1. Which of the following one operation with addresses is not possible?
(a) Addition of two addresses (pointers) (b) ++ (c) –
2. Which of the following one operation is possible with address holding (pointers)?
(a) Multiplication of addresses or multiplication of address with a constant
(b) Division of address with a constant
(c) ++
(d) None of the above
3. The declaration int *arrp[3]; is nothing but .
(a) Integer array (b) Integer pointer array (c) One of the above
4. Pointer to pointer can be denoted as .
(a) **p (b) p** (c) *p
5. The following program will show the error message .
int p=2; char *c=&p;
(a) Expression syntax in function main
(b) Non-portable assignment
(c) Suspicious pointer conversion
(d) None of the above
6. int p=2,*k; int *c=&p; k=c; we can say that .
(a) Pointer c and k are pointing to p
(b) Pointer k pointing to p and c
(c) Only pointer c pointing to p
7. int s,*j,**kk; j=&s; kk=&j; we can say that .
(a) kk is pointer of pointer (b) j is pointer of pointer (c) k is only pointer
8. A pointer is a memory variable that stores a .
(a) memory address (b) value (c) None of the above
9. Pointer can have any name that is legal for other .
(a) Variable (b) Keyword (c) Header file
10. Pointer is declared in the same fashion like other variable, but it is always denoted by
operator.
(a) * (b) & (c) #
11. Pointers save the .
(a) Memory space (b) Value (c) None of the above
12. Execution time with pointer is faster because .
(a) Data is manipulated with the address (b) Data is manipulated (c) None of the above
13. Pointers are used with data structures. They are useful for representing and multi-
dimensional arrays.
(a) Two-dimensional (b) Multi-dimensional (c) None of the above
M09_ITL-ESL4791_02_SE_C09.indd 198 12/22/2012 5:04:02 PM
Pointers II-199
14. In the statement int *x is a (an) .
(a) Integer pointer (b) Float pointer (c) Unsigned pointer
15. Declaration int *x; tells to the compiler that it holds the .
(a) Address of any integer variable
(b) Address of any float variable
(c) Address of any char variable
16. The indirection operator (*) is also called .
(a) Indirection or dereferencing operator (b) Declaration
(c) Deference (d) None of the above
17. When the pointer is dereferenced, the indirection operator indicates that the .
(a) Value at that address stored can be accessed
(b) Address of variable can be accessed
(c) None of the above
18. Fill the columns after ++ and −− operations.
Data Type Initial Address Operation Address After Operations
Int i=2 4046
++
-- 4048 4044
char c='x' 4053
++
-- 4054 4052
float f=2.2 4058
++
-- 4062 4054
Long l=2 4060
++
-- 4064 4056
19. Array name by itself is an address or .
(a) Pointer (b) Value (c) Identifier
20. Array of pointers is nothing but .
(a) A collection of addresses (b) A collection of values (c) All of the above
21. The pointer variables also have an , which is stored in another pointer.
(a) Name (b) Address (c) Value
22. The pointer variable containing address of another pointer variables is called .
(a) Pointer to pointer (b) Pointer (c) Double pointer
23. The memory address of the variable is a (an) .
(a) Constant (b) Pointer constant (c) Integer
24. Which of the following statement is true after execution of following program?
void main()
{
int a[5]={2,3},*c;
clrscr();
c=a;
(*c)--;
printf("%d",a[0]);
}
M09_ITL-ESL4791_02_SE_C09.indd 199 12/22/2012 5:04:02 PM
II-200 Programming Concepts
(a) The value of a[0] will be 1. (b) The value of a[0] will be 2.
(c) The value of a[1] will be 2. (d) None of the above.
25. The fastest way to exchange two rows in a two-dimensional array is .
(a) Exchange the addresses of each element in the two rows.
(b) Exchange the elements of the two rows.
(c) Store the addresses of the rows in an array of pointers and exchange the pointers.
(d) None of the above.
26. Which is the correct way to declare a pointer?
(a) int *ptr; (b) * int ptr;
(c) int ptr*; (d) int_ptr x;
27. What will be the result of the following program?
void main()
{
int a=8,b=2,c,*p;
c=(a=a+b,b=a/b,a=a*b,b=a-b);
p=&c;
clrscr();
printf(" %d",++*p);
}
(a) 45 (b) 46 (c) 47
28. What will be the resulting string after execution of following program?
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char *str1, *str2, *str3;
str1="The Capital of India is";
str2="!!ihleD weN";
str3="Bangalore";
strncat(str1,strrev(str2),strlen(str3));
clrscr();
puts(str1);
}
(a) The Capital of India is New Delhi
(b) The Capital of India is New Delhi!!
(c) The Capital of India is Bangalore
(d) None of the above
M09_ITL-ESL4791_02_SE_C09.indd 200 12/22/2012 5:04:02 PM
Pointers II-201
29. What will be the values of variables a and b after execution?
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
int a,*b=&a,**c=&b;
a=5;
**c=15;
*b= **c;
clrscr();
printf("A=%d, B=%d",a,*b);
}
(a) A = 15, B = 15 (b) A = 15, B = 5 (c) A = 15, B = 16 (d) None of the above
30. What will be the value of variable a1 and a2 after execution?
#include <stdio.h>
#include <conio.h>
void main()
{
int a1,a2,c=3,*pt;
clrscr();
pt=&c;
a1=3*(c+5);
a2=3*(*pt+5);
printf(" a1=%d a2= %d",a1,a2);
}
(a) a1 = 25, b1 = 24 (b) a1 = 24, b1 = 24 (c) a1 = 24, b1 = 26 (d) None of the above
31. What will be the value of x after execution of the following program?
#include <stdio.h>
#include <conio.h>
void main()
{
int x,*p;
p=&x;
*p=2;
clrscr();
printf(" Value of x=%d",x);
}
(a) x = 2 (b) x = 0 (c) x = 65504 (d) None of the above
M09_ITL-ESL4791_02_SE_C09.indd 201 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