Set - 2

Question 1 :

Write down the equivalent pointer expression for referring the same element a[i][j][k][l] ?

Answer :

a[i] == *(a+i)
a[i][j] == *(*(a+i)+j)
a[i][j][k] == *(*(*(a+i)+j)+k)
a[i][j][k][l] == *(*(*(*(a+i)+j)+k)+l)

 


Question 2 :

Which bit wise operator is suitable for checking whether a particular bit is on or off ?

Answer :

The bitwise AND operator. Here is an example:

enum {
	KBit0 = 1,
	KBit1,
	…
	KBit31,
};

if ( some_int & KBit24 )
	printf ( "Bit number 24 is ON\n" );
else
	printf ( "Bit number 24 is OFF\n" );

 


Question 3 :

Which bit wise operator is suitable for turning off a particular bit in a number ?

Answer :

The bitwise AND operator, again. In the following code snippet, the bit number 24 is reset to zero.

some_int = some_int & ~KBit24;

 


Question 4 :

Which bit wise operator is suitable for putting on a particular bit in a number ?

Answer :

The bitwise OR operator. In the following code snippet, the bit number 24 is turned ON:

some_int = some_int | KBit24;

 


Question 5 :

Does there exist any other function which can be used to convert an integer or a float to a string ?

Answer :

Some implementations provide a nonstandard function called itoa(), which converts an integer to string.

#include 
char *itoa(int value, char *string, int radix);

DESCRIPTION
The itoa() function constructs a string representation of an integer.

PARAMETERS
value: Is the integer to be converted to string representation. 

string: Points to the buffer that is to hold resulting string. The resulting string may be as long as seventeen bytes. 

radix: Is the base of the number; must be in the range 2 - 36. 

A portable solution exists. One can use sprintf():

char s[SOME_CONST];
int i = 10;
float f = 10.20;
sprintf ( s, "%d %f\n", i, f );