Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- An array was declared and set to 6 elements = {5, 3, 4, 17, 22, 19}
- A pointer to an int was initialized to minimart function call
- The addresses of array[0] & array[2] were passed as references to the function
- Within the function, the contents of the addresses were compared by use of pointers
- if contents of a were less than the contents of b (5 < 4)
- then return a
- Otherwise, return b
- Thus b was returned
- This function gives the starting address of ptr, which is at pos[2] of the array
- ptr[1] = 9
- would change the contents from pos[3] to 9
- {5, 3, 4, 9, 22, 19}
- ptr += 2
- ptr increments up from pos[2] to pos[4]
- *ptr = -1
- contents of pos[4] will be changed to -1
- {5, 3, 4, 9, -1, 19}
- *(array+1) = 79;
- Since the array pointer automatically starts from 0,
- seperate from the ptr pointer,
- pos[1] of the array would be changed to 79
- {5, 79, 4, 9, -1, 19}
- For the following function, we are substracting
- the pos of elements for both array and ptr
- The difference from &array[5] - ptr would print 1
- because a substraction of pos[5] - pos[4] would occur (6 - 5 = 1)
- note: elements of arrays have a start of 0, not 1.
- First swap function receives addresses for both array[0] and array[1]
- They are passed as arguments into pointer a and b
- The swapping of addresses occurs between pointers a and b
- AND NOT to the actual array itself. Also as soon as the function terminates;
- a and b will not not exist anymore in the memory
- Thus, the array will remain the same {5, 79, 4, 9, -1, 19}
- Second swap function receives addresses for both array[0]and array[2]
- This function directly changes the contents of both elements of the array
- Temp varible will hold the value of 5
- Contents of pos[0] will be changed to 4
- Contents of pos[2] will be changed to 5
- The updated array will be {4, 79, 5, 9, -1, 19}
- The for loop will print the elements in order
- So compiler would display 4 79 5 9 -1 19
- End of program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement