Industrial Training




C Tips and Tricks-1



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 ;

}



What's wrong with the following declaration:

char* ptr1, ptr2 ;

I 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 ;



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.



What is the difference between const char *p, char const *p, and char* const p ?

'const char *p' and 'char const *p' are the same, i.e. p points to a constant character. On the other hand, 'char* const p' means p is a constant pointer pointing to a character which means we cannot change the pointer p but we can change the character which p is pointing to.



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.



How can I write a general-purpose swap without using templates?

Ans: Given below is the program which uses the stringizing preprocessor directive ## for building a general purpose swap macro which can swap two integers, two floats, two chars, etc.

#define swap( a, b, t ) ( g ## t = ( a ), ( a ) = ( b ), ( b ) = g ## t )

int gint;

char gchar;

float gfloat ;

main( )

{

int a = 10, b = 20 ;

char ch1 = 'a' , ch2 = 'b' ;

float f1 = 1.12, f2 = 3.14 ;

swap ( a, b, int ) ;

printf ( "\na = %d b = %d", a, b ) ;

swap ( ch1, ch2, char ) ;

printf ( "\nch1 = %c ch2 = %c", ch1, ch2 ) ;

swap ( f1, f2, float ) ;

printf ( "\nf1 = %4.2f f2 = %4.2f", f1, f2 ) ;

}

swap ( a, b, int ) would expand to,

( gint = ( a ), ( a ) = ( b ), ( b ) = gint )



On including a file twice I get errors reporting redefinition of function. How can I avoid duplicate inclusion?

Ans: Redefinition errors can be avoided by using the following macro definition. Include this definition in the header file.

#if !defined filename_h

#define filename_h

/* function definitions */

#endif

Replace filename_h with the actual header file name. For example, if name of file to be included is 'goto.h' then replace filename_h with 'goto_h'



How to write a swap( ) function which swaps the values of the variables using bitwise operators.

Ans: Here is the swap( ) function.

swap ( int *x, int *y )

{

*x ^= *y ;

*y ^= *x ;

*x ^= *y ;

}

The swap( ) function uses the bitwise XOR operator and does not require any temporary variable for swapping.



Hi I am Pluto.