Wednesday, December 10, 2008

Difference in passing by Reference & by Address

most of time passing by reference and by address seems same, or most of us consider them same but their is difference in both technique.
When we pass a variable to a function by reference,it mean we are passing alias of variable. and in address we are passing their actual address through pointer variable.


Example will clear the concept

void add(int&)
void main()
{
int i=10;
add(i);
cout<<"\ni="
}
void add(int& a)
{
a+=20;
}


<
*********-------->>>>>><<<<<<<<---------------------******************
by address

void add(int*)//prototype
void main()

{
int i=10;
add(&i);
cout<<"\ni="<}
void add(int* a)
{
*a=*a+20;
}


output i=30

Wednesday, December 3, 2008

Playing wid Pointers

Defination->

Pointer are the
Derived Type Data Type,Which points to the address of the variable.

Common use of Pointer->

1.Accessing Array element.
2.Passing Argument to the function,when function need to modify original argument.
3.Obtaining memory from the system.
4.Creating Data structure like Link List

Address of operator & ->

The address of variable can be find using 'address of operator- &'
example :
int i;
int* ptr;
ptr=&i;
cout<<& i;

~ The Automatic variable are stored in stack,So while accessing address they appear in decending order.
~External variable address appear to be in acceding order,As they store in Heap.

@ There is difference in
'address of operator &' and 'reference operator &'
~ 'A of OP &' precedes a
variable name in a variable declaration.
~'R of OP &' follows type in in function prototype or definition.

example .... passing variable to function :-

void cal(int &); //prototype declaration for 'passing by reference'
void cal(int *) ;//prototype declaration for 'passing by address'

int var;
call(var);//passing by reference
call(&var);// passing by address

void call(int& v);//function definition in 'pass by reference'
void call(int* ptr)//function definition in 'pass by address'

~passing by reference mean passing an
alias of variable.
~While in pointer we pass address of variable.

int* PTR->
The asterisk means pointer to.
~We say variable PTR is pointer to Int.

Above we have said that pointer is Data type, then why we do not define the variable as
POINTER ptr; as in case of INT data type.

@ Reason-
becoz need to know kind of variable pointer points to, As compiler has to calculate the memory allocation as in case of array. Example is given below.


Accessing the Variable->
~ variable are access using its address rather than its name
Ex:
int i;
int* ptr;
ptr=& i;
cout<<*ptr;//value of variable will be display. *ptr -
when asterisk sign is used in front of variable name it is called indirect operator.
~
it mean value of variable pointed to by.Indirection operator some time also call dereference operator.

int *ptr//declartion: pointer to int
*ptr =3//indirect or dereference operator



Friday, June 27, 2008

some points on declartion,intialinsation and data type in C

* When ever their is conflict b/ween local and global variable ,local always win. i.e if we have declare two variable with same name & we call the variable then the local variable value will be shown.

*Their is 4 kind of scope of variable where it exits-:


1.function
2.block
3.file
4.prototype

* In declaration the nature of the variable is define no storage is allocated,while in definition the variable is created or assigned the value.

*Redefinition is error while redeclaration is not error.


>Data type Point

*A 16 bit compiler means that when it compiles a c program it generate machine language code that is targeted towards working on a 16 bit microprocessor,like intel 8086/88

~vc++ is 32 bit compiler target for Intel Pentium.

*Range of intezer constant depends on the compiler

~for 32 bit compiler range=-2147483648 to 2147483647
~for 16 bit compiler range =-36768 to 36767

* Intel itanium processor does not support 16 bit code.

* The highest bit (16th or 32 bit) is use to store the sign of integer 1 is for -ive & 0 for +ive.

* Signed char has range -127 to 128.

* Float occupies 4 bits
~ range -3.4 e 38 to 3.4 e 38

* tc/tc++ perform all float operation using a s/ware piece called 'Floating Point Emulator'.
~This emulator becomes part of exe file,thereby increasing its size.

* There is always one extra number on the -ive side. This is because a -ive number is always store in 2's compliment.