Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.

Similar presentations


Presentation on theme: "Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays."— Presentation transcript:

1 Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays

2 Pointers in C Declaration int *p; /* p is a pointer to an int */ A pointer in C is always a pointer to a particular data type: int*, double*, char*, etc. Operators *p -- returns the value pointed to by p &z -- returns the address of variable z

3 Declaring Pointer variables int *ptr; ptr is a pointer variable that points to an int variable char *cp; cp is a pointer variable that points to a character variable double *dp; dp is a pointer variable that points to a double variable * is referred to as the indirection operator, or the dereference operator

4 Address Operator & int object; int *ptr; object = 4; ptr = &object pointer variable ptr points to variable object ptr = null pointer not = 0

5 Example Define local variables: int object; int *ptr; Now, let’s assign values to them: object = 4; ptr = &object *ptr = *ptr + 1; The last statement is equivalent to: object = object + 1

6 Example: Parameter Passing by Value #include void Swap(int firstVal, int secondVal); int main() { int valueA = 3; int valueB = 4; Swap(valueA, valueB); return 0; } void Swap(int firstVal, int secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = firstVal; firstVal = secondVal; secondVal = tempVal; return; } Snapshot before return from subroutine

7 Example: Parameter Passing by Reference #include void NewSwap(int *firstVal, int *secondVal); int main() { int valueA = 3; int valueB = 4; NewSwap(&valueA, &valueB); return 0; } void NewSwap(int *firstVal, int *secondVal) { int tempVal; /* Holds firstVal when swapping */ tempVal = *firstVal; *firstVal = *secondVal; *secondVal = tempVal; return; } Snapshots during the exchange

8 Scanf( ) function Recall reading from the keyboard in C: scanf(“%d”, &input); Why do we use &input ?

9 Pointer Example #include int IntDivide(int x, int y, int *quoPtr, int *remPtr); int main() { int dividend; /* The number to be divided */ int divisor; /* The number to divide by */ int quotient; /* Integer result of division */ int remainder; /* Integer remainder of division */ int error; /* Did something go wrong? */ printf("Input dividend: "); scanf("%d", &dividend); printf("Input divisor: "); scanf("%d", &divisor); error = IntDivide(dividend,divisor,&quotient,&remainder); if (!error) /* !error indicates no error */ printf("Answer: %d remainder %d\n", quotient, remainder); else printf("IntDivide failed.\n"); } int IntDivide(int x, int y, int *quoPtr, int *remPtr) { if (y != 0) { *quoPtr = x / y; /* Modify *quoPtr */ *remPtr = x % y; /* Modify *remPtr */ return 0; } else return -1; }


Download ppt "Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays."

Similar presentations


Ads by Google