Ques 66. How do I define a pointer to a function which returns a char pointer?
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 ; }
Is it helpful?
Add CommentView Comments
Ques 67. What's wrong with the following declaration: char* ptr1, ptr2 ; get errors when I try to use ptr2 as a pointer.
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 ;
Is it helpful?
Add CommentView Comments
Ques 68. How to use scanf( ) to read the date in the form of dd-mm-yy?
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.
Is it helpful?
Add CommentView Comments
Ques 69. Why the output of sizeof ( 'a' ) is 2 and not 1 ?
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.
Is it helpful?
Add CommentView Comments
Ques 70. Can we use scanf( ) function to scan a multiple words string through keyboard?
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.
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.