Sie sind auf Seite 1von 7

Variable :- Consider the following declaration:Int a=10; What does the above declaration indicates?

It says that a is the name of the memory location which will contain integer types of data. The value stored at this location is 10. A 10 1000 Thus the above declaration tells about:1) A is the name of the memory location 2) This memory location will contain integer types of values 3) Value 10 is stored at this memory location. Location Number:- Every memory location has its name and corresponding location number. In the above example 1000 is the location number. Location number is also called address of the memory. We can say that memory can be represented either by location name or the location number. This location number is not fixed. At any location the above integer value can be stored. But we should be clear that it will occupy only 2 bytes (since it is an integer value). Thus if it starts from 1000 then it will keep the space till 1002 location number. The above integer variable may also stored at location 2000 also( Because as we told earlier location number is not fixed. So this time till 2002 location number a will take space in memory). Consider the following program:Void main(){ Int a = 10; Printf(Address of a = %u, &a); Printf(Value of a = %d,a); } Now if we consider the above diagram then the output will be:Address of a = 1000 Value of a = 10 Address of Operator:- & is called Address Of operator. Thus &a means address of a. Address is an unsigned number and for that we use %u specifier. Value at Address Operator:- * is called value at address operator. It returns the value stored at the particular address. * is also called indirection operator. It is the name of the location 10 is the value at this location 1000 is the location number

Consider the following program: Void main(){ Int a = 10; Printf(Address of a = %u, &a); Printf(Value of a = %d,a); // value at memory location a will be displayed Printf(Value of a = %d,*(&a)); } Since &a is the address of a so *(&a) will be treated as:*(&a) =*(address of a) =*(1000) =value at (1000) =10 Thus the output of the above program will be:1000 10 10 Pointer Variable:Consider the following diagram:A 10 1000 b 1000 5000

In the above diagram we can observe that b is also one variable which contains the address of variable a i.e 1000. Variable b is also having its own address i.e 5000. Here b is considered as some special type of variable which contains the address of some ordinary variable, not the value like integer or floating point or char value is stored in b. Here b is called as pointer variable. Thus we can define pointer variable as:Pointer is a special type of variable which contains the address of some ordinary variable. Thus for the above diagram a and b will be declared as:Int a = 10; Int *b; // This tells the compiler that b is a pointer variable which will store the address of integer value. i.e. b points to integer. B = &a; Now consider the following program:-

Void main(){ Int a = 10; Int *b; B = &a; Printf(Address of a = %u, &a); Printf(Address of a = %u, b); // since b is containing the address of a Printf(Value of a = %d,a); // value at memory location a will be displayed Printf(Value of a = %d,*(&a)); // value at the address of a Printf(Value of a = %d,*b); // The value at the address contained in j i.e *b = *(&a) Printf(Address of b = %u, &b); // &b means address of b Printf(Value of b = %d,b); // value at the location b } Output:Address of a = 1000 Address of a = 1000 Address of a = 1000 Value of a = 10 Value of a = 10 Value of a = 10 Address of b = 2000 Value of b = 1000 Now Consider the following declarations:Int *I; // I is a pointer variable which will contain address of variable pointed by integer Char *c; // c is a pointer variable which will contain address of variable pointed by character Float *f; // f is a pointer variable which will contain address of variable pointed by floating point In all the above declarations, I, c and f are declared as a pointer variable which will hold the address. Pointer is a variable which can contain the address of any type of variable whether it is an integer or char or floating point variable. The pointer can contain the address of even another pointer variable also. The above declaration does not tells that I will take integer value or c will take character type value or so on. Now Question is what is the size of pointer variable? Answer:- Since The pointer contains the address and it is number so the size of any pointer whether it contains the address of integer or char or float, all pointers will be of size 2 in case of DOS/Turboc/Turboc++ but in case of windows environment like in Visual C++ the size of pointer is 4 bytes because the size of integer is 4 bytes in VC++.

Now again consider the following diagram and declaration:A 10 aa 1000 aaa 5000

1000 Void main(){

5000

6000

Int a = 10; Int *aa; Int **aaa; Aa = &a; // aa is a pointer variable which contains the address of integer variable a. Aaa = &aa; // aaa is also a pointer variable and it contains the address of pointer variable aa. } Now if we write the following then, &a = address of a. Aa = address of a because aa = &a &aa = address of aa Aaa = address of aa *aaa = *(address of aa) = value at (address of aa) = value at (5000) = 1000 = address of a Thus *aaa means address of a. &aaa = address of aaa Thus if we append the following code in the above program then Printf(Address of a = %u,&a); // 1000 Printf(Address of a = %u,aa); //1000 Printf(Address of a = %u,*aaa); //1000 Printf(Address of b = %u,&b); // 5000 Printf(Address of b = %u,aaa); // 5000 Printf(Address of aaa = %u,&aaa); // 6000 Now again think about the following lines:A = value of a will be shown = 10 *(&a) = value at address a = 10 *aa = *(&a) because aa = &a so the value will be = value at address of a = 10

**aaa = *(*(&aa)) because aaa = &aa Thus **aaa = *(*(address of aa)) = *(value at (address of aa)) = *(1000) // because value at address is 1000 = value at 1000 = 10 Therefore **aaa will give the value of a i.e 10 Thus we can append the following lines of code and get the output:Printf(Value of a = %d,a); Printf(Value of a = %d,*(&a)); Printf(Value of a = %d,*aa); Printf(Value of a = %d,**aaa); Q int ***aaaa; means? Ans:- aaaa is a pointer which contains the address of another pointer variable which contains the address of some another pointer variable. Just like Aaaa = &aaa;

Important Concept:F i

1000 Int i; Int *ii; Float f; Float *ff; Ii = &I; Ff = &f;

1001

1002

1003

1004

2000

2001

2002

ii 2000 3000

ff 1000 5000

Here ii contains the address of integer I and ff contains address of floating point f. Now address for I is started from 2000 and since integer is of 2 bytes so address will goes till 2002. Similarly floating point address starts from 1000 and goes till 1004 because float is of 4 bytes. Now we can observe that ii contains only 2000 and ff contains only 1000. This 2000 0r 1000 is called base address. Points contains

the base address only. But we can get all other address also by using the pointer(How? We will see in the array with pointer chapter ) Now we will study pointers that will be used with functions:Call By Value and Call By Reference:When we call a function at that time we can pass the arguments to the function also. The arguments can be passed either in the following two ways:i) We can send the actual values of the arguments------We call it Call By Value or ii) We can send the address of the arguments------We call it Call By Reference

Call By Value:Here the value of each actual parameters/arguments in the calling function is copied into corresponding formal arguments of the called function. By using this method, if we change the formal parameters in the called function then there will be no effect on the values of the actual arguments in the calling function. Consider the following program:Void main(){ Int x = 20; Calling Function Actual Arguments Int y = 30; Swap(x,y); Printf(After swapping the value of x =%d,x); Printf(After swapping the value of y =%d,y); } Void swap( int p, int q){ Int temp; Temp = p; P = q; Q = temp; Called Function Formal Parameters Printf(p=%d,p); Printf(q=%d,q); }

Here in the main function the value of x and y will not changed therefore the output will be given x = 20 and y = 30 because we are sending the value of x and y not the address of x and y so one copy of x and y are sent not the original value.

Call By Reference Here the address of the actual arguments in the calling function are copied into formal arguments of the called function. Using the formal arguments in the called function we can make changes in the actual parameters of the calling function. Consider the following program:Void main(){ Int x = 20; Int y = 30; Swap(&x,&y); // Here we are passing the address of x and y not the value of x and y. Since address is passed so any change will effect the original value of that location. Printf(After swapping the value of x =%d,x); Printf(After swapping the value of y =%d,y); } Void swap( int *p, int *q){ // Since at the time of calling the function swap we are passing address and as we know that only pointer variable has a capability to store the address therefore in this called function there must a pointer variable declaration like *p and *q which can contain address of x and y respectively.) Int temp; Temp = *p; // here temp contains value at address pointed by p i.e 20 *P = *q; // value at address pointed by p will contain now value at address pointed by q *Q = temp; // value at address pointed by q will contain the value of temp } Output:After swapping the value of x = 30 After swapping the value of y = 20 The way a function can return int, char or float, similarly Function can return pointers also. This can be explained easily by using the following program:Void main(){ Int *p; Int *fun(); // This declaration says that function fun will return pointer which points integer variable P = fun(); // Since fun will return address of integer variable therefore there must be pointer variable which will collect that address. Therefore p is declared as pointer. Printf(%u,p); // Here the value of p is nothing but an address therefore u specifier. } Int *fun(){ // Function definition based on the function declaration Int I =10; Return(&i); } Note:- Pointers with Arrays, Structure.will be discussed after this.

Das könnte Ihnen auch gefallen