{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

The and would be called for example as swapab object

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: are put and are temp = X1; on the stack and used as the X1 = X2; addresses of X1 and X2 iin X1 and X2 n X2 = temp; the function. the } and would be called, for example, as Swap(A,B); Object Programming Object Programming In C, the addresses must be explicitly put on the stack stack Swap(&A,&B); and the function must reference these addresses and as pointers and dereference them to change the data pointed at. data void Swap(int *X1, int *X2) void Swap(int *X1, int { int temp; We are not changing the We temp = *X1; pointers, just what they *X1 = *X2; point at. point *X2 = temp; } Object Programming Which do you prefer? W hich void Swap(int& X1, int& X2) void Swap(int X1, int { int temp; void Swap(int *X1, int *X2) void Swap(int *X1, int { int temp; temp = X1; X1 = X2; X2 = temp; temp = *X1; *X1 = *X2; *X2 = temp; } typedef int* intPtr; intPtr and then } Swap(A,B); We could have used We Swap(&A,&B); They are mechanically (almost) the same. void Swap(intPtr X1, intPtr X2) void Swap(intPtr X1, intPtr Old C programmers think their way is best since it Old makes you realise what is going on within the function – that you are not just manipulating that copies of A and B. and Object Programming in our C version. Let's talk about passing array arguments. Let's W e all know that arrays are passed by reference We – the address of the 0th element is put on the the stack. stack. For the array Consider Object Programming int A[10] = {1,2,3,4,5,6,7,8,9,10}; we can sum the entire array as SumArray(A,10) or SumArray(&A[0],10) int SumArray(int arr, int n) arr, int { int i, sum=0; } (C++ allows this pointer approach but avoid it.) int SumArray(int arr, int n) int arr, int { int i, sum=0; for (i=0;i<n;i++) sum += arr[i]; sum arr[i return sum; Object Programming For the array For int A[10] = {1,2,3,4,5,6,7,8,9,10}; we can sum the entire array as SumArray(A,10) or SumArray(&A[0],10) But the same function can also sum the last nine But elements using elements SumArray(&A[1],9) That is, an array name and an address seem to That be equivalent. be Object Programming } for (i=0;i<n;i++) sum += arr[i]; sum arr[i return sum; Object Programming In fact, a pointer type can also be referenced like an array. an So int A[10]; int* B=A; means the pointer B gets the address of the array means gets A (its name). Now B can be used just like A could. Now can Object Programming If we use *B we get the value of A[0]. *B we A[0]. But B[0] iis also the same as referring to A[0]. But B[0] s A[0]. Similarly B[5] iis the same variable as A[5]. Similarly B[5] s A[5]. Note that B[10] is still off the end of the array. Note B[10] All the problems inherent in accidentally referencing elements of an array which have not been allocated by the compiler worsen if we get into the habit of using pointers as addresses of other variables. other int A; int* B=&A; // need the & to get address and then use variables such as B[7], B[50],etc. and B[7] B[50] But it gets worse!!!! Major danger. Obj...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern