Monday, January 5, 2009

Pointers in 'c'

The pointers are used to point to a given location. They point to a location which contain the information of the datatype which is the datatype of its own. The pointer can be of any datatype used in ‘C’. It is represented like the declaration of the other variables but the difference is that it is preceded by an asterisk sign during declaration. During definition if the value is assigned to it then it is preceded by asterisk or if it is assigned the address then only the variable is given. The value of only the variable is equal to the value of the ordinary variable preceded by ampersand(&).
e.g. int *x;
This is declaration of pointer.
Let x be the normally declared variable and y be the pointer.
Int x;
Int *y;
X=5;-is the declaration of normal variable.
*y=5;-is the declaration of pointer variable.
X=*y;
Y=&x;
These are the relations of normal variable and pointer variable.
e.g.
void main()
{
Int x;
Int *y;
x=5;
*y=x;
Printf(“x=%dy=%d”,x,*y);
}
In the above program the value of both x and y will be same.i.e.5.
void main()
{
Int x;
Int *y;
x=5;
y=&x;
Printf(“x=%dy=%d”,x,*y);
}
In the above program also the value of both x and y will be same.i.e.5.
Now we will see how to accept the values in the pointer.
void main()
{
Int *y;
Printf(“Enter a value”);
Scanf(“%d”,y);
Printf(“%d”,*y);
Getch();
}
In the above program in scanf the instead of ‘&y’ we have used only ‘y’ which means that in pointer ‘y’ is equivalent to ‘&x’(where x is normal variable) and while printing instead of ‘y’ we give ‘*y’.
In array the array variable written alone serves as pointer. It points to the first variable of the array.
e.g.
int x[5];
if only x is used then it means that it is used as a pointer to the first number of the array.it is mostly used in functions when there is a need to send an array.
The array can be assigned to a single pointer value.
e.g. int x[5];
int *y;
y=x[];
Then accessing the other value of x is b incrementing the pointer by 2.
For float the incrementing is 2 and for character it is 1.

Friday, January 2, 2009

Functions

A function is a program which is written outside the main program. The main itself is a function. The function has its unique name. It is declared outside the main program in the beginning. Its definition is given at the beginning after its declaration or after the main program. It is called in the main program. Due to this function the program can be understood easily.
e.g.
Void add()
{
…..
}
Void main()
{

Add();
….
}
or
void add();
void main()
{
….
Add();


}
Void add()
{
….
….
}
e.g.
void hi()
{
Printf(“hi”);
}
void main()
{
Hi();
Getch();
}
The functions can be bigger than the main. In the above program the function ‘hi()’ is called. The control goes to the function, prints hi then goes to the main function then exits.
The return statement is also used in the function if a value is to be returned to the main function. Then the return type will not be void instead the data type of the returning value.
e.g.
int add(int x,int y);
void main()
{
Int x,y,z;
X=5;
Y=6;
Z=add(x,y);
Printf(“%d”,z);
Getch();
}
Int add(int x,int y)
{
Int z;
Z=x+y;
Return z;
}
In the above program the value of x and y are taken to the function added there and returned which is taken in z and printed.

Watch tv!