Thursday, November 20, 2008

Difference between a null pointer, a NULL macro, the ASCII NUL character and a null string

90. What's the difference between a null pointer, a NULL macro, the ASCII NUL character and a null string?

Ans: A null pointer is a pointer which doesn't point anywhere. A  NULL macro is used to represent the null pointer in source code. It has a value 0 associated with it. The ASCII NUL character has all its bits as 0 but doesn't have any relationship with the null pointer. 
The null string is just another name for an empty string "".
System Utility
Sparse Matrix...
A sparse matrix is one where most of its elements are zero. There is no precise definition as to know whether a matrix is sparsed or not, but it is a concept which we all can recognize intuitively. The natural method of representing matrices in memory as two-dimensional arrays may not be suitable for sparse matrices. That is one may save space by storing only those entries which may be nonzero. If this is done, then the matrix may be thought of as an ordered list of non-zero elements only. Information about a non-zero element has three parts:
an integer representing its row,
an integer representing its column and
the data associated with this element.
That is, each element of a matrix is uniquely characterized by its row and column position, say i, j. We might store that matrix as a list of 3-tuples of the form (i, j, data), as shown below,
Although the non-zero elements may be stored in the array in any order, keeping them ordered in some fashion may be advantageous for further processing. Note that above array is arranged in increasing order of the row number of non-zero elements. Moreover, for elements in the same row number, the array is arranged in order of increasing column number.

91. Pointers
What does the error "Null Pointer Assignment" mean and what causes this error?

Ans: The Null Pointer Assignment error is generated only in small and medium memory models. This error occurs in programs which attempt to change the bottom of the data segment. In Borland's C or C++ compilers, Borland places four zero bytes at the bottom of the data segment, followed by the Borland copyright notice "Borland C++ - Copyright 1991 Borland Intl.". In the small and medium memory models, a null pointer points to DS:0000. Thus assigning a value to the memory referenced by this pointer will overwrite the first zero byte in the data segment. At program termination, the four zeros and the copyright banner are checked. If either has been modified, then the Null Pointer Assignment error is generated. Note that the pointer may not truly be null, but may be a wild pointer that references these key areas in the data segment.

Data Structures
How to build an expression trees ?

Ans: An expression tree is a binary tree which is built from simple operands and operators of an (arithmetic or logical ) expression by placing simple operands as the leaves of a binary tree and the operators as the interior nodes. If an operator is binary , then it has two nonempty subtrees, that are its left and right operands (either simple operands or sub expressions). If an operator is unary, then only one of its subtrees is nonempty, the one on the left or right according as the operator is written on the right or left of its operand. We traditionally write some unary operators to the left of their operands, such as "-" ( unary negation) or the standard functions like log( ), sin( ) etc. Others are written on the right, such as the factorial function ()!. If the operator is written on the left, then in the expression tree we take its left subtree as empty. If it appears on the right, then its right subtree will be empty. An example of an expression tree is shown below for the expression ( -a <>

92. Can we get the remainder of a floating point division ?

Ans : Yes. Although the % operator fails to work on float numbers we can still get the remainder of floating point division by using a function fmod( ). The fmod( ) function divides the two float numbers passed to it as parameters and returns the remainder as a floating-point value. Following program shows fmod( ) function at work.
#include 
main( )
{
printf ( "%f", fmod ( 5.15, 3.0 ) ) ;
}
The above code snippet would give the output as 2.150000.

93. How to extract the integer part and a fractional part of a floating point number?

Ans: C function modf( ) can be used to get the integer and fractional part of a floating point.
#include "math.h"
main( )
{
double val, i, f ;
val = 5.15 ;
f = modf ( val, &i ) ;
printf ( "\nFor the value %f integer part = %f and fractional part = %f",
val, i, f ) ;
}
The output of the above program will be:

For the value 5.150000 integer part = 5.000000 and fractional part =
0.150000

94. How do I define a pointer to a function which returns a char pointer?

Ans:
char * ( *p )( ) ;
or
typedef char * ( * ptrtofun )( ) ;
ptrtofun p ;
Here is a sample program which uses this definition.
main( )
{
typedef char * ( * ptrtofun ) ( ) ;
char * fun( ) ;
ptrtofun fptr ;
char *cptr ;
fptr = fun ;
cptr = (*fptr) ( ) ;
printf ( "\nReturned string is \"%s\"", cptr ) ;
}
char * fun( )
{
static char s[ ] = "Hello!" ;
printf ( "\n%s", s ) ;
return s ;
}

95. What's wrong with the following declaration: char* ptr1, ptr2 ; get errors when I try to use ptr2 as a pointer.

Ans: char * applies only to ptr1 and not to ptr2. Hence ptr1 is getting declared as a char pointer, whereas, ptr2 is being declared merely as a char. This can be rectified in two ways :
char *ptr1, *ptr2 ;
typedef char* CHARPTR ; CHARPTR ptr1, ptr2 ;

96. How to use scanf( ) to read the date in the form of dd-mm-yy?

Ans: To read the date in the form of dd-mm-yy one possible way is,
int dd, mm, yy ;
char ch ; /* for char '-' */
printf ( "\nEnter the date in the form of dd-mm-yy : " ) ;
scanf( "%d%c%d%c%d", &dd, &ch, &mm, &ch, &yy ) ;
Another way is to use suppression character * is as follows:
int dd, mm, yy ;
scanf( "%d%*c%d%*c%d", &dd, &mm, &yy ) ;
The suppression character '*' suppresses the input read from the standard input buffer for the assigned control character.

97. Why the output of sizeof ( 'a' ) is 2 and not 1 ?

Ans: Character constants in C are of type int, hence sizeof ( 'a' ) is equivalent to sizeof ( int ), i.e. 2. Hence the output comes out to be 2 bytes.

98. Can we use scanf( ) function to scan a multiple words string through keyboard?

Ans: Yes. Although we usually use scanf( ) function to receive a single word string and gets( ) to receive a multi-word string from keyboard we can also use scanf( ) function for scanning a multi-word string from keyboard. Following program shows how to achieve this.
main( )
{
char buff[15] ;
scanf ( "%[^\n]s", buff ) ;
puts ( buff ) ;
}
In the scanf( ) function we can specify the delimiter in brackets after the ^ character. We have specified '\n' as the delimiter. Hence scanf( ) terminates only when the user hits Enter key.

99. How to set the system date through a C program ?

Ans: We can set the system date using the setdate( ) function as shown in the following program. The function assigns the current time to a
structure date.
#include "stdio.h"
#include "dos.h"
main( )
{
struct date new_date ;

new_date.da_mon = 10 ;
new_date.da_day = 14 ;
new_date.da_year = 1993 ;

setdate ( &new_date ) ;}

No comments:

Blog List