WILL THE FOLLOWING PROGRAM WORK??? SUPPOSE THAT THE BASE ADDRESS OF i IS 2008 AND THAT OF a IS 7006.

THE ADDRESS OF ii IS 7602 AND THAT OF aa IS 9118. /* PROGRAM */ main() { int i = 54; float a = 3.14; char *ii,*aa; ii = &i; aa = &a; printf("\n printf("\n printf)"\n printf("\n } ANSWER: Here ii and aa have been declared as char pointers. Still the statements ii = &i and aa = &a will work. Once again the addresses 2008 and 7006 get stor ed in ii and aa which are printed through the first two printf()s. However, the porgram flaters at the next two printf()s.This is so since ii is a character poi nter *ii gives the value at the address 2008 and dot the one present at 2008 and 2009.Similarly, *aa gives the value at 7006 and not the one comtained in 7006,7 007,7008 and 7009. THIS IS BECAUSE A CHARACTER VARIBLE OCCUPIES ONLY ONE MEMORY LOCATION WHEN WE ACCESS THEM, ONLY ONE MEMORY LOCATION IS REFERRED TO. From this we come to know that iv we wish to access an integer value sto red in a variable using its address it is necrssary that the address be stored i n an integer pointer. Likewise if we wish to accrss a float value stored in a ba riable using its address it is necessary to store the address in a float pointer .

address cintained in address cintained in value at the address value at the address

ii = %u",ii); aa = %u",aa); contained in ii = %d",*ii); contained in aa = %d",*aa);

PASSING ADDRESSES TO FUNCTIONS: There are 2 methods of doing so. They are 1) call by value(sending the values of the arguements). 2) call by reference(dending the addresses of the ts.) 1) In the first menthod i.e. "call by value", the 'value' of each arguement in the calling function is copied into corresponding formal arguenents of the c alled fuction. With this method changes made to the formal arguenents in the cal led fuction will have no effect on the values of the actual arguements in the ca lling function. EXAMPLE: THE FOLLOWING PROGRAM ILLUSTRATES THE ' CALL BY VALUE'. /* main() { PROGRAM */

arguemen

x). x = y. } OUTPUT: . *x = *y.e. This means that using the formal arguements in the called functi on we can make changes in the actual arguements of the calling function. EXAMPLE: THE FOLLOWING PROGRAM ILLUSTRATES THIS FACT: /* PROGRAM */ main() { int a int b swapr(&a. b = %d".b).a).x). "call by reference" the address of the actual arguements in the calling function are copied into the formal aruements of the c alled function. y = t.int int swapv(a. } OUTPUT: x = 20 y = 10 a = 10 b = 20 NOTE: Here the values of a and b aremain unchanged even after exchanging the v alues of x and y. printf(" \n printf(" \n } a = 10. /* fucntion subprogram */ swapr(int *x. printf("\n x = %d".a). b = 20. = %d".&b). printf("\n y = %d". printf(" \n a printf(" \n b } = 10.y).int y) { int t.y). a = %d". t = x. 2) In the second method i. t = *x.b). *y = t. /* fuction subprogram */ swapv(int x. This me thod is faster than call by value method. = 20. = %d".int *y) { int t. printf("\n x = %d".b). printf("\n y = %d".

As against this. printf(\n c = %u".a b x y = = = = 20 10 20 10 SOLVED PROBLEMS: WHAT WILL BE THE OUTPUT OF THE FOLLOWING PROGRAMS: FIRST PROGRAM 1] main() { int i = -5. since 'j' s' address is bein g passed to 'junk()'.i. } /* subprogram */ junk(int i. any change in 'junk()' gets reflected back in 'main()'.20). Naturally. this change wil l not be reflexted back in main(). without absolutely any conflict. whereas 'j' is dexlared as a pointer to an 'int'.j). if( i >= 45) return(p). c = check(10.*q. q = *j. } OUTPUT: i = -5 j = 4 EXPLANATION: One doubt immediately that comes to the mind is that " can we us e same variables in different functions?". in 'junk()' 'i' is declared as an ordinary int . intj) { int *p. junk(i.c).j = -2. p = &i. printf("\n i = %d j = %d". int *j) { i = i * i. else . SECOND PROGRAM 2] { main() int *c. *j = *j * *j. While calling the fuction 'junk()' the value of i and the address of j are passed to it. } /* subprogram */ check(int i. Even though the value of 'i' is changed to 25 in junk(). the two sets of 'i' and 'j' are two totally different sets of variables.&j). Thus. Yes. by all means.

main() { float *jamboree(). The integers bing passed to 'check()' are collexted in 'i' and 'j'.5. printf("\n c = %u ". } 4] Assume that the value of p is stored at 5498. The function 'c heck()' is not capable of returning an integer pointer. It appears that this address would be collected in 'c' in ' main()'. and then wouldbe printed out. and then their addresses are assigned to 'p' and 'q'.q). and return return either tha address stored in 'p' or the a ddress stored in'q'. We must make the following modifications is the program to make it work p roperly. if(i >= 45) return(p).c).*q. p = &i. Then in the next statement the conditional operators test the valu e of 'i' against 45.q). And there lies the error. All that it can return i s an ordinary integer. c = check(10. printf(" \n before call = %u". float p = 23. return(r).return(q).20). } OUTPUT: error message:non portable pointer assignment in main. q = jamboree(&p). MODIGFIED PROGRAM: main() { int *c: int *check(). q = &p.*q. } /* subprogram */ float *jamboree(float *r) { r = r + 1. } /* subprogram */ int *check(int i.int j) { int *p. Thus just declaring 'c' as an integer ponter is not suffi cient. q = &j. } OUTPUT: q before call = 5498 . printf(" \n after call = %u". else return(q). EXPLANATION: The reason for the error is simple.

x). a decalration float '*jamboree( )' is necessary in 'main()'.x).500000 value of k = c original value in x = 1002 original value in y = 2004 . The return statement then returns this address 55 02 back to 'main()'. printf("\n original value in y = %u".i).z). } SOLUTION: suppose 'i'.y). the output would be: value of i = 3 value of j = 1. printf("\n new value in z = %u". y++. printf("\n value of i = %d". is stored in q and then pronted out through the printf(). printf("\n original value in z = %u". let us first learn some pointer arithmetic. printf("\n value of k = %c". sinc e every float is 4 bytes long.*x. float j = 1. char k = 'c'.j).*z. when you execute the program this may turn out be some other address). z++.y).z).'j' and 'k' are stored in memeory at addresses 1002. **************** POINTERS AND ARRAYS ***************** To be able to see what pointers have got to do with arrays. x = &i. which will return a float pointer. which tells the compoler that down the line there e xists a function called 'jamboree()'.q after call = 5502 EXPLANATION: In 'main()'. printf("\n value of j = %f".k). printf("\n\n original value in x = %u". When 'r' is incremented it would become 5502. z = &k. y = &j. At this juncture 'r' contains 5498(when we ran the program it was 5498.5. Since a float pointer is being returned. It means 'q ' is a variable capable of holding the address of a float. When 'jamboree()' is called th e address of p is sent to it and collected in 'r'. Consider the following example: /* program */ main() { int i = 3. Through 'q = &p' the address of 'p'. This i s the value of 'q' before 'jamporee()' is called. q has been declared as a float pointer. x++.*y. 2004 and 5006. printf("\n new value in x = %u". printf("\n new value in y = %u". why a step of 4? This is because 'r' is a float pointer and in incrementing it by 1 it would point to the next float which would be present 4 bytes hence.

1004 is original value in x p lus 2.. since an int is always 2 bytes long.*a). int j. they would never work out. 2. (a) addition of two pointers.20. it can be decremented as well. j + j + j + 4.50}. Thus. CAUTION: Do not attempt the following operations on pointers.30.original value in z = 5006 new value in x = 1004 new value in y = 2008 new value in z = 5007 Observe the last three lines of the outut. to earlier locations. the following operations can be performed on a pointer: 1] addition of a number to a pointer.This so happens because every time a pointer is incremented it points to the i mmediately next location of its type. j++) { printf("\n %d ". 3. accessing array elements by pointers is always faster than accessing them by subscripts. (b) Multiplying a pointer with a number. That is why.*k. 5.*k. j < 5. 9. Similarly 'y' points to an address 4 locati ons after the current location and 'z' points 1 location after the current locat ion. and 5007 is original balue in z plus 1.*j.40. This is a very important result and can be effectively used while passing t he entire array to a function. *j.. The way a pointer can be incremented. int j = j = j = k = 2] i = &i. it points to an address two locations after the current location. when the integer pointer x is incremented. for(j = 0. (c) Dividing a pointer with a number. 6. a++. /********************** SAMPLE PROGRAMS ********************/ WHAT WILL BE THE OUTPUT OF THE FOLLOWING PROGRAMS: 1] main() { int a[] = {10. j j j 4. 1. subtraction of a number from a pointer. ADVANTAGE: While handling arrays. int j = j = j = k = i = &i. } } OUTPUT: . 2008 is original value in y plus 4.

the base address of the array a[] is stored in 'j'.*n. } OUTPUT: Error message: illegal use of pointer in function main EXPLANATION: 'j' and 'k' have been decalred as pointer variables.5. In other words. '*(n + 24) + *(n + 0)'. This is then printed out. we get its base addre ss.*k). printf("\n %d %d". k = a + 4.3. } OUTPUT: 100 300 EXPLANATION: 'n[]' has been declared as an array capable of holding 25 elemen ts numbvered form 0 to 24.*j. it would be unable to remember the begi nning of the array. Then comes the most important part. To begin with.*(n + 24) + *(n + 0)). which is 200 in our case. n[0] = 100. Whene ver we mention the name of the array. once declared is its base address. Therefore. Thus. Thus. we get its base address(i. it flashes the reeor saying 'Lvalue required' so that ++ operator can change it. Since C doesn. Similarly. which is out putted next.t perform bounds checking on an array. j = a. 'a++'. And 'a++' attempts to change this base address. 'n = 1' giv es the address of the next element of the array. 3] main() { int n[25].e. wh ich C won't allow because if it does so. Hence the error mess age. Take a look at the next expression.1. Since vbalue of a cannot be changed through ++. Multiplication or division of a pointer is not allowed.'*n' would give the value at this base addre ss. 2] main() { float a[] = { 13. which would contain the addresses of floats. There is no problem upto this. The next statement is perfectly acceptable. 'n' gives the address of the zeroth element. the address of the 4th float form the base ad dress is dtored in 'k'. '*(n + 0) ' would give 100 and the addition of the two would result into 300.the 'printf()' statement.ERROR MESSAGE: Lvalue required in function main EXPLANATION: Whenever we mention the name of the array. which in this case is 100. the 'printf()' should print the valu e of this base address. first time through the loop.5}. The next two statements aree erroneous.24. 'j' and 'k' are float pointe rs. The problem lies in the n ext statement.is call ed Lvalue.4.5. j = j * 2. and so on. Then 100 and 200 are assigned to 'n[0]' and 'n[24]' r expectively. Anything which can change . printf("\n %f %f".in compiler's language .1.5. k = k/2. float *j. This is because the only operations that can be performed on pointers are addition and subtracti on. . '*(n+24)' woul d give the value at this address. n[24] = 200. address of th e '0th' element of the array).*k.

for(i = 0. From this address if we subtract 4. i. Firs t time through the for loop '*k' would result into 10.e the address 4002. 5] main() { int a[] = {2.50}. the loop prints out the rest of the e lements of the array. i++) { *(a + i) = a[i] + i[a]. k++. 4004.*k). is stored in k.*k.e. whi ch has been declared as a variable capable of holding an integer's address. int i.10}. for(i = 0.*(i + a)).20. ARRAY ELEMENT b[0] b[1] b[2] b[3] b[4] VALUE 10 20 30 40 50 MEMORY LOCATION 4002 4004 4006 4008 4010 The expression '&b[4]' gives the addressof 'b[4]'(4010 in the above case ). k = &b[4] . we get 4002. i <= 4. 4002. i++) { printf("%d". This address. hence is occupying 2 locations. address of the integer which is 4 integers to the left of the integer whos e address is 4010. Similarly. } } OUTPUT: 4 8 12 16 20 EXPLANATION: Imbibe the following three facts and the program becomes very simple to understand: (a) Mentioning the name of the array gives the base address . Now. int i. 'k++' then increments k such that it contains the address of the next i nteger. Or did you expect to get 400 6? Remembre that by subtraction 4 form 4010 what we mean is: get the address of an integer which is 4 integers to the left of the integer whose address is 4010. i.30.8. } } OUTPUT: 10 20 30 40 50 EXPLANATION: The elements of the array are stored in contiguous memory locati ons and each element is an integer. i <= 4.6. which is 20. value at the address in 'k'.4] main() { int b[] = {10. printf("%d".e.40.4.4. i.

Here *(y) += 2 is evaluated forst and then the result of this expression is a ssigned to x. Thus. (c) On adding 1 to the address of an integer. on assigning a new value to x. for(i = 0. But '*(a + i)' is same as '*(i + a)'. the = oerator assigns 7 to x. } } f(int x. let us now try to understand the program.a[i]. The first time through the for loop *(y) gives 5. '*(a + i)' = 'a[i]' + 'i[a]' is nothing but 'a[i]' = 'a[i]' + 'a[i]'. to which 2 is ad ded and the result is stored at *(y). Therefore 'a[i]' must be same as 'i[a]'. 6] main() { int a[5] = {2. which mean s value of ith integer from the base address.int *y) { x = *(y) += 2.thus 'a[i]'. array element value memory location a[0] 2 b 5 4008 . (b) Array elements are stored in contiguous memory locations.8. during every call to f(). } OUTPUT: 2 7 4 9 6 11 8 13 10 15 EXPLANATION: After initialising the array when the control enters the for loo p. we get the address of the next integer. the array elem ent a[0] in main remains unchanged. i++) { f(a[i]. Now. Therefore the expression used in the for loop. if the expression 'a[i]' is s ame as '*(a + i)' then '*(i + a)' must be same as 'i[a]'. In f() these are collected in variables x and y. Finally.b).'i'th element form the base address. int i. '*(i + a)' and 'i[a]' refer to the same element.b = 5.6. It means 7 is addigned to b.&b) printf("\n %d %d". the function f() gets called with value of a[i] and the address of b.4. i < 5. Thus. b's value ke eps getting updated whereas there is no change in the values of the array elemen ts. With those facts clearly laid out. thus all that is done in the for loop is each array element is doubled and then printedout through 'p rintf()'.of the array. then comes the expression x = *(y) += 2. However. when we say a[i] internally C converts it to '*(a + i)'.10}. '*(a + i)'. Remember that internally C always accesses array elements using poi nters.

5.6}.x 2 7] main() { int a[5] = {2.4. y 4008 . change(a). int i.3.

Sign up to vote on this title
UsefulNot useful