1. What is dangling pointer (wild pointer), when would it happen, what are the problems & how to avoid it?
Ans:
i) It is a pointer pointing to a dead location or some arbitrary location.
ii) It would happen in several scenarios:
Ex1 : returning the address of a local variable from a function
Ex2 : not initializing a pointer
Ex3 : If a pointer still points to an object which is dead
iii) Accessing/ modifying unknown/unreserved locations which may corrupt other data or other data may corrupt our data (as we use unreserved locations)
iv) It can be avoided by:
Ex1: Initializing a pointer to some variable’s address or to NULL.
Ex2: Assigning NULL to a pointer as soon the object to which it points to is dead.
Ex3 : Not returning the address of a local variable from a function.
2. What is the size of a pointer?
Ans:
2 bytes ( in a 16-bit compiler ), 4 bytes ( in a 32-bit compiler ).
Because a pointer just holds address of a memory location.
3. What happens when a pointer is incremented/decremented?
Ans:
It moves by the length of the datatype of the pointer & is called the scale factor.
4. How to dereference a void pointer?
Ans:
By typecasting, otherwise how would the pointer know – from how many bytes the data has to be retrieved.
5. What are the applications of pointers?
Ans:
Some of the applications of pointers are:
Call by address
Passing entire array directly to a function
Dynamic memory allocation
Returning more than one value from a function
Pointer notation is faster than array notation (so wherever we use array notation it is better to use equivalent pointer notation )
Ex1 : &a[i] ( a + i
Ex2: a[i] ( *(a + i)
Storing the address of a function
6. Why an array variable cannot be put on left side of = (assignment operator)
Ans: Because it is a constant pointer
7. What is nested function call?
Ans: Calling a function from another function. Eg: printf(“%d”, sum(a, b) );
8. How to allocate memory for a matrix-using array of pointers?
Ans:
int* m[10];
for(i=0;i<r;i++)
m[i] = (int*)malloc( c * sizeof( int ) );
Where ‘r’ stands for number of rows and ‘c’ stands for number of columns
9. How to allocate memory for a matrix-using a double pointer ( DMA for 2-D array)
Ans:
int **m;
m = (int**)malloc( r * sizeof( int ) );
for(i=0;i<r;i++)
m[i] = (int*)malloc( c * sizeof( int ) );
10. Why %u is used to print address of a memory location?
Ans:
In a 16-bit compiler, an address would be in the range of 1- 65535.
We know that %d ‘s range is -32768 to +32767
It the address assigned to a variable is greater than +32767 then it would result in garbage output. Hence %u is used ( whose range is 0 to +65535).
11. What arithmetic operations are possible on pointers?
Ans:
Pointer + Integer
Pointer - Integer
Pointer - Pointer
12. What are the different types of constant pointers?
Ans:
A non-constant pointer to a constant data
A constant pointer to a constant data
A constant pointer to a non-constant data
13. Why ampersand is required while reading integers using scanf()?
Ans:
int n ;
scanf(“%d”, &n);
We know that call by address is required to get a new value into ‘n’ through a called function.
14. Why ampersand is not required while reading strings using scanf()?
Ans:
char st[20];
scanf(“%s”, st);
We know that ‘st’ is nothing but base address of the array. So it is anyhow call by address.
15. What are function pointers?
Ans:
We know that every variable has some address and it can be stored in some pointer variable. Similarly every function has some address and it can be stored in some function pointer.
One of its application areas is : for writing TSR programs
16. How to return more than one value from a function?
Ans:
There are several ways:
i) Using a dummy array
ii) Using pointers
iii) Using dynamic memory allocation
17. What are the different types of function call mechanisms in C?
Ans:
i) call by value
ii) call by address
18. When to use realloc()?
Ans:
When ever we want to add data to the adjacent locations of previously allocated memory.
19. What is the difference between malloc() and calloc()?
Ans:
1. malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments.
2. malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
20. How to pass arrays to functions?
Ans:
By passing the base address of the array, and a number representing the number of values.
21. If malloc() or calloc() or realloc() cannot allocate requested amount of memory, what do they return?
Ans:
NULL
22. What is the advantage of Dynamic memory allocation?
Ans:
For efficient usage of memory.
23. Const char *p , char const *p What is the difference between the above two?
1) const char *p - Pointer to a Constant char ('p' isn't modifiable but the pointer is)
2) char const *p - Also pointer to a constant Char
However if you had something like:
char * const p - This declares 'p' to be a constant pointer to an char. (Char p is modifiable but the pointer isn't)
Ans:
i) It is a pointer pointing to a dead location or some arbitrary location.
ii) It would happen in several scenarios:
Ex1 : returning the address of a local variable from a function
Ex2 : not initializing a pointer
Ex3 : If a pointer still points to an object which is dead
iii) Accessing/ modifying unknown/unreserved locations which may corrupt other data or other data may corrupt our data (as we use unreserved locations)
iv) It can be avoided by:
Ex1: Initializing a pointer to some variable’s address or to NULL.
Ex2: Assigning NULL to a pointer as soon the object to which it points to is dead.
Ex3 : Not returning the address of a local variable from a function.
2. What is the size of a pointer?
Ans:
2 bytes ( in a 16-bit compiler ), 4 bytes ( in a 32-bit compiler ).
Because a pointer just holds address of a memory location.
3. What happens when a pointer is incremented/decremented?
Ans:
It moves by the length of the datatype of the pointer & is called the scale factor.
4. How to dereference a void pointer?
Ans:
By typecasting, otherwise how would the pointer know – from how many bytes the data has to be retrieved.
5. What are the applications of pointers?
Ans:
Some of the applications of pointers are:
Call by address
Passing entire array directly to a function
Dynamic memory allocation
Returning more than one value from a function
Pointer notation is faster than array notation (so wherever we use array notation it is better to use equivalent pointer notation )
Ex1 : &a[i] ( a + i
Ex2: a[i] ( *(a + i)
Storing the address of a function
6. Why an array variable cannot be put on left side of = (assignment operator)
Ans: Because it is a constant pointer
7. What is nested function call?
Ans: Calling a function from another function. Eg: printf(“%d”, sum(a, b) );
8. How to allocate memory for a matrix-using array of pointers?
Ans:
int* m[10];
for(i=0;i<r;i++)
m[i] = (int*)malloc( c * sizeof( int ) );
Where ‘r’ stands for number of rows and ‘c’ stands for number of columns
9. How to allocate memory for a matrix-using a double pointer ( DMA for 2-D array)
Ans:
int **m;
m = (int**)malloc( r * sizeof( int ) );
for(i=0;i<r;i++)
m[i] = (int*)malloc( c * sizeof( int ) );
10. Why %u is used to print address of a memory location?
Ans:
In a 16-bit compiler, an address would be in the range of 1- 65535.
We know that %d ‘s range is -32768 to +32767
It the address assigned to a variable is greater than +32767 then it would result in garbage output. Hence %u is used ( whose range is 0 to +65535).
11. What arithmetic operations are possible on pointers?
Ans:
Pointer + Integer
Pointer - Integer
Pointer - Pointer
12. What are the different types of constant pointers?
Ans:
A non-constant pointer to a constant data
A constant pointer to a constant data
A constant pointer to a non-constant data
13. Why ampersand is required while reading integers using scanf()?
Ans:
int n ;
scanf(“%d”, &n);
We know that call by address is required to get a new value into ‘n’ through a called function.
14. Why ampersand is not required while reading strings using scanf()?
Ans:
char st[20];
scanf(“%s”, st);
We know that ‘st’ is nothing but base address of the array. So it is anyhow call by address.
15. What are function pointers?
Ans:
We know that every variable has some address and it can be stored in some pointer variable. Similarly every function has some address and it can be stored in some function pointer.
One of its application areas is : for writing TSR programs
16. How to return more than one value from a function?
Ans:
There are several ways:
i) Using a dummy array
ii) Using pointers
iii) Using dynamic memory allocation
17. What are the different types of function call mechanisms in C?
Ans:
i) call by value
ii) call by address
18. When to use realloc()?
Ans:
When ever we want to add data to the adjacent locations of previously allocated memory.
19. What is the difference between malloc() and calloc()?
Ans:
1. malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments.
2. malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.
20. How to pass arrays to functions?
Ans:
By passing the base address of the array, and a number representing the number of values.
21. If malloc() or calloc() or realloc() cannot allocate requested amount of memory, what do they return?
Ans:
NULL
22. What is the advantage of Dynamic memory allocation?
Ans:
For efficient usage of memory.
23. Const char *p , char const *p What is the difference between the above two?
1) const char *p - Pointer to a Constant char ('p' isn't modifiable but the pointer is)
2) char const *p - Also pointer to a constant Char
However if you had something like:
char * const p - This declares 'p' to be a constant pointer to an char. (Char p is modifiable but the pointer isn't)