1 / 8

Lab guide #6

Lab guide #6. C Pointers Review & Lab assignments. Key points. Pointers and pointer operators. Pass arguments to functions by reference by using pointer. sizeof Operator and Pointer Arithmetic. The close relationships among pointers, arrays and strings. Pointers to functions.

talli
Download Presentation

Lab guide #6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab guide #6 C Pointers Review & Lab assignments

  2. Key points • Pointers and pointer operators. • Pass arguments to functions by reference by using pointer. • sizeof Operator and Pointer Arithmetic. • The close relationships among pointers, arrays and strings. • Pointers to functions.

  3. Pointers and pointer operators • Pointer: • Contains address of a variable that has a specific value (indirect reference) • Definitions: int *myPtr1, *myPtr2; • Operators: • Address operator: & • Indirection/dereferencing operator: * • * and & are inverses, They cancel each other out int y = 5; int *yPtr; yPtr = &y; /* yPtr gets address of y */ *yptr = 7; /* changes y to 7 */

  4. Pass address of argument using & operator • Arrays are not passed with & • Used as alias/nickname for variable inside of function /*Function definition*/ void double( int *number ) { *number = 2 * ( *number ); } int main (void) { int a =5; /*Function call*/ double( &a ) printf(“%d /n”, a) } • Const qualifiers: variable can not be changed int *const myPtr = &x; /*Constant pointer*/ const int *myPtr = &x; /*Constant int*/ const int *const Ptr = &x; /*Cons. pointer point to Cons. int*/ Pass arguments to functions by reference by using pointer.

  5. Sizeof: Returns size of operand in bytes • Can be used for program that depend on data type sizes and run on different system • Pointer Arithmetic: • ++, --, +,+=,-,-= • Operations meaningless unless performed on an array • Unit: base on array type and system int v[]; vPtr2 = v[ 2 ]; vPtr = v[ 0 ]; vPtr2 – vPtr = 2 vPtr = 3000; vPtr += 2; /*vPtr = vPtr2 = 3008; 1 unit = 4 bytes*/ sizeof Operator and Pointer Arithmetic.

  6. Arrays and pointers closely related • Array name like a constant pointer • Pointers can do array subscripting operations • Equalities between pointer bPtr and array b[] • bPtr = b; • bPtr = &b[0]; • bPtr[3] = b[3]; • *( bPtr + 3 ) = b[3]; • Arrays of pointers • char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; The close relationships among pointers, arrays and strings.

  7. Function pointers • Contains address of function • Function pointers can be • Passed to functions • Stored in arrays • Assigned to other function pointers • Two definition ways: • int *compare( int a, int b ) • Rerturn a pointer to int • int ( *compare )( int a, int b ) • Return an int Pointers to functions

  8. 1. Relationship between pointers and arrays: • Fig. 7.24 in the lecture note • 2. Pointers to function: • Fig.7.26, 7.28in the lecture note Lab assignments • 3. Write a function largest that returns the largest value of an integer array and its position. For example, if an array contains { 13, 25, 80, 189, 30, 66, 4}, the function largest( ) should return the value 189 and its positions 3 (starting from 0). You are supposed to decide the arguments of the function. Suggestions (not mandatory) • Largest value can be returned by function • The positions of largest value can be returned by using pointers to pass arguments to function by reference

More Related