Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
Practical Paper
Industrial Training
C Tips and Tricks-2
What do you mean by a Dangling Pointer?
Ans: A pointer pointing to an object that has been destroyed is called a Dangling pointer. For example, the pointer ptr in the function given below is a dangling pointer.
main( )
{
int *ptr ;
int *f( ) ;
ptr = f( ) ;
printf ( "%d", *ptr ) ;
}
int *f( )
{
int a = 10 ;
return ( &a ) ;
}
Here it appears that address of a would be collected in ptr and through *ptr we would be able to access 10. This however is not possible because by the time address of a is collected in ptr, a is already dead. So you have a pointer containing an address, and the object at this address is long dead. Thus ptr becomes dangling pointer.
Can you analyse the following statement? What is being printed?
printf ( "%d", ( x.a[i] )( p, q, r ) -> k ) ;
Ans: x is a structure variable. One of its elements is an array of pointers to functions. We are picking the ith pointer from this array. Using this pointer we are making a call to the function and passing it the arguments p, q, and r. The function in turn is returning a pointer to a structure, say z. We are printing an integer k which is an element of the structure z. Can you now write the prototype of the function being called?
The prototype would be:
struct z *f ( int p, int q, int r ) ;
Tips About typedef of Pointers to Functions
1. typedef int ( * ( * arr2d_ptr ) ( ) ) [3][4] ;
arr2d_ptr p ;
Ans: p is a pointer to a function returning a pointer to a 2-D int array.
2. typedef int ( * ( * ( * ptr2d_fptr ) ( ) ) [10] )( );
ptr2d_fptr p ;
Ans: p is a pointer to a function returning a pointer to an array of 10 pointers to function returning an int.
3. typedef char ( * ( * arr_fptr[3] ) ( ) ) [10] ;
arr_fptr x ;
Ans: x is an array of 3 pointer to function returning a pointer to an array of 10 chars.
4. typedef float* ( * ( * ( * ptr_fptr ) ( ) ) [10] )( );
ptr_fptr q ;
Ans: q is a pointer to function returning a pointer to an array of 10 pointers to functions returning a float pointer.
Why is it not possible to scan strings from keyboard in case of array of pointers to string?
Ans: When an array is declared, dimension of array should be specified so that compiler can allocate memory for the array. When array of pointers to strings is declared its elements would contain garbage addresses. These addresses would be passed to scanf( ). So strings can be received but they would get stored at unkown locations. This is unsafe.
Suppose I have a structure having fields name, age, salary and have passed address of age to a function fun( ). How I can access the other member of the structure using the address of age?
Ans:
struct emp
{
char name[20] ;
int age ;
float salary ;
} ;
main( )
{
struct emp e ;
printf ( "\nEnter name: " ) ;
scanf ( "%s", e.name ) ;
printf ( "\nEnter age: " ) ;
scanf ( "%d", &e.age ) ;
printf ( "\nEnter salary: " ) ;
scanf ( "%f", &e.salary ) ;
fun ( &e.age ) ;
}
fun ( int *p )
{
struct emp *q ;
int offset ;
offset = ( char * ) ( & ( ( struct emp * ) 0 ) -> age ) - ( char * ) ( ( struct emp* ) 0 ) ;
q = ( struct emp * ) ( ( char * ) p - offset ) ;
printf ( "\nname: %s", q -> name ) ;
printf ( "\nage: %d", q -> age ) ;
printf ( "\nsalary: %f", q -> salary ) ;
}
How do I print the contents of environment variables?
Ans:. The following program shows how to achieve this:
main( int argc, char *argv[ ], char *env[ ] )
{
int i = 0 ;
clrscr( ) ;
while ( env[ i ] )
printf ( "\n%s", env[ i++ ] ) ;
}
main( ) has the third command line argument env, which is an array of pointers to the strings. Each pointer points to an environment variable from the list of environment variables.
Suppose we have a floating point number with higher precision say 12.126487687 and we wish it to be printed with only precision up to two decimal places. How can I do this?
Ans. This can achieved through the use of suppression char '*' in the format string of printf( ) which is shown in the following program.
main( )
{
int p = 2 ;
float n = 12.126487687 ;
printf ( "%.*f",p, n ) ;
}