Monday, March 27, 2006

Void Pointer

In C, Void pointer( void * ) is an generic data type. There are times when you write a function but do not know the datatype of the returned value. When this is the case, you can use a void pointer.

int func(void *Ptr);
main()
{
char *Str = "abc";
func(Str);
}

int func(void *Ptr)
{
printf("%s\n", Ptr);
}

Note:
You cant do pointer arithmatic on void pointers.

Volatile

The compilation system tries to reduce code size and execution time on all machines, by optimizing code. It is programmer responsibility to inform compilation system that certain code ( or variable ) should not be optimized.

A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread.

Usage of volatile
1. An object that is a memory-mapped I/O port
2. An object variable that is shared between multiple concurrent processes
3. An object that is modified by an interrupt service routine
4. An automatic object declared in a function that calls jmpfunc and whose
value is-changed between the call to jmpfunc and a corresponding call to
topjmp

Thursday, March 16, 2006

Solve This

if( X )
{
printf("Hello");
}
else
{
printf("World");
}

Question: What should be the value of "X" so that the output is HelloWorld?

Wednesday, March 15, 2006

How to Change 0x1234 to 0x12

Program:
void main( )
{
int a = 0x1234;

clrscr( );
printf( "Value is:%x", a>>8 );
getch( );

}

Answer:
Value is:12

Swapping Two Numbers

Program:
void main( )
{
int a=10, b=20;

a^=b^=a^=b;
clrscr( );
printf( "Value of a %d, Value of b %d", a, b );
getch();
}

Ans: Value of a 20, Value of b 10

Function Pointer

Function Pointers are pointers, i.e. variables, which point to the address of a function. You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks.

When you want to call a function Do() at a certain point called label in your program, you just put the call of the function Do() at the point label in your source code. Then you compile your code and every time your program comes up to the point label, your function is called. Everything is ok.

But what can you do, if you don't know at build-time which function has got to be called? What do you do, when you want to decide it at runtime? Maybe you want to use a Callback-Function or you want to select one function out of a pool of possible functions. However you can also solve the latter problem using a switch-statement, where you call the functions just like you want it, in the different branches.

Sizeof in C

Program:
#include
#include
main( )
{
char a[]="hello";
char b[]={ 'h', 'e', 'l','l','o' };

printf( "Size a=%d\t b=%d ", sizeof( a ), sizeof( b ) );
getch( );
}

What would be the output of the above program?

Ans:
Size a=6 b=5
yes, because the array a contains a '\0'( NULL character ) at the end of the string. So it returns 6. But the array b is formed character by character. So there is noway for NULL. SO it returns 5.