Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. An array was declared and set to 6 elements = {5, 3, 4, 17, 22, 19}
  2.  
  3. A pointer to an int was initialized to minimart function call
  4.  
  5. The addresses of array[0] & array[2] were passed as references to the function
  6.  
  7. Within the function, the contents of the addresses were compared by use of pointers
  8.  
  9. if contents of a were less than the contents of b (5 < 4)
  10. then return a
  11. Otherwise, return b
  12. Thus b was returned
  13. This function gives the starting address of ptr, which is at pos[2] of the array
  14.  
  15. ptr[1] = 9
  16. would change the contents from pos[3] to 9
  17. {5, 3, 4, 9, 22, 19}
  18.  
  19. ptr += 2
  20. ptr increments up from pos[2] to pos[4]
  21.  
  22. *ptr = -1
  23. contents of pos[4] will be changed to -1
  24. {5, 3, 4, 9, -1, 19}
  25.  
  26. *(array+1) = 79;
  27. Since the array pointer automatically starts from 0,
  28. seperate from the ptr pointer,
  29. pos[1] of the array would be changed to 79
  30. {5, 79, 4, 9, -1, 19}
  31.  
  32. For the following function, we are substracting
  33. the pos of elements for both array and ptr
  34. The difference from &array[5] - ptr would print 1
  35. because a substraction of pos[5] - pos[4] would occur (6 - 5 = 1)
  36. note: elements of arrays have a start of 0, not 1.
  37.  
  38. First swap function receives addresses for both array[0] and array[1]
  39. They are passed as arguments into pointer a and b
  40. The swapping of addresses occurs between pointers a and b
  41. AND NOT to the actual array itself. Also as soon as the function terminates;
  42. a and b will not not exist anymore in the memory
  43. Thus, the array will remain the same {5, 79, 4, 9, -1, 19}
  44.  
  45. Second swap function receives addresses for both array[0]and array[2]
  46. This function directly changes the contents of both elements of the array
  47. Temp varible will hold the value of 5
  48. Contents of pos[0] will be changed to 4
  49. Contents of pos[2] will be changed to 5
  50. The updated array will be {4, 79, 5, 9, -1, 19}
  51.  
  52. The for loop will print the elements in order
  53. So compiler would display 4 79 5 9 -1 19
  54. End of program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement