C Programming: Pointer Operations Pointer Operations Non-void pointer variables pointer to the same

  • View
    2

  • Download
    0

Embed Size (px)

Text of C Programming: Pointer Operations Pointer Operations Non-void pointer variables pointer to the same

  • Course Overview

    C Programming: Advanced

    C Programming: Advanced ● Instructor

    �❍ Jack Straub �❍ 425-888-9119 �❍ jstraub@centurytel.net �❍ http://faculty.washington.edu/jstraub

    ● Grading

    �❍ 80% Attendance �❍ 70% Successful completion of homework �❍ 70% Grade on final exam

    ● Recommended Textbook

    �❍ C: A Reference Manual, Fifth Edition, Samuel Harbison and Guy Steele. ISBN: 0-13-089592-X

    ● Schedule

    �❍ See web site

    ● Additional Recommended Readings �❍ See web site

    Course Overview

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...%20Work/AdvancedC/Binders/010Introduction/100Intro.html [3/29/2008 10:01:02 AM]

    http://faculty.washington.edu/jstraub file:///C|/Documents%20and%20Settings/Jack%20Straub/My%20Documents/School%20Work/AdvancedC/Binders/schedule.html file:///C|/Documents%20and%20Settings/Jack%20Straub/My%20Documents/School%20Work/AdvancedC/Binders/schedule.html#additional_readings

  • Homework Overview

    Homework Overview

    Homework Overview ● Schedule

    �❍ 9 Assignments �❍ Due at the start of each class �❍ Late submissions are not accepted

    ● Grading

    �❍ 0 to 20 points; 180 points total �❍ 70% of 180 = 126 points minimum passing grade

    ● Submission

    �❍ All assignments submitted via the class drop box:

    Homework Drop Box (home page)

    ● General

    �❍ Two weeks for each assignment (except exercise 1) �❍ Sample solutions discussed/published at the start of each class

    Homework Overview

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My...ork/AdvancedC/Binders/010Introduction/200Homework.html [3/29/2008 10:01:02 AM]

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%20Documents/School%20Work/AdvancedC/Binders/index.html#drop_box file:///C|/Documents%20and%20Settings/Jack%20Straub/My%20Documents/School%20Work/AdvancedC/Binders/index.html#drop_box

  • Structures

    Structure Declaration

    #define NAME_LEN (50) struct personnel_rect { char last[NAME_LEN + 1]; char first[NAME_LEN + 1]; char initial[2] int ident; };

    Structure Definition

    struct personnel_rect employee;

    Direct Selection Operator (.)

    strcpy( employee.last, "shmoe" ); strcpy( employee.first, "joe" ); strcpy( employee.initial, "q" ); employee.ident = 100;

    Structures

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...l%20Work/AdvancedC/Binders/020Review/110Structures.html [3/29/2008 10:01:02 AM]

  • Structures

    Structures and typedef typedef struct personnel_rec { char last[51]; char first[51]; char initial[2]; int ident; } EMP_REC_t, *EMP_REC_p_t;

    static void print_employee( EMP_REC_p_t emp );

    int main( int argc, char **argv ) { EMP_REC_t employee; . . . print_employee( &employee ); . . . }

    static void print_employee( EMP_REC_p_t emp ) { printf( "%s, %s %s: %d\n", emp->last, emp->first, emp->initial, emp->ident ); }

    Structures

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...k/AdvancedC/Binders/020Review/115StructuresTypedef.html [3/29/2008 10:01:02 AM]

  • Structures

    Pointers to Structures

    static void print_employee( struct personnel_rec *emp );

    int main( int argc, char **argv ) { struct personnel_rec employee; . . . print_employee( &employee ); . . . }

    Indirect Selection Operator (->)

    static void print_employee( struct personnel_rec *emp ) { printf( "%s, %s %s: %d\n", emp->last, emp->first, emp->initial, emp->ident, ); }

    Structures

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...dvancedC/Binders/020Review/120PointersToStructures.html [3/29/2008 10:01:03 AM]

  • Pointers

    Pointers Definition:

    The address of an object of a particular type, or a variable which may be used to represent such an address.

    typedef

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%2...k/AdvancedC/Binders/020Review/200PointersDefinition.html [3/29/2008 10:01:03 AM]

  • void Pointers

    void Pointers void pointers (also called generic pointers) can hold any address except the address of a function.

    int inx = 376; double dnx = 3.14; void *ptr = &inx; . . . printf( "%d\n", *(int *)ptr ); ptr = &dnx; printf( "%lf\n", *(double *)ptr );

    void pointers

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My...0Work/AdvancedC/Binders/020Review/210VoidPointers.html [3/29/2008 10:01:03 AM]

  • NULL Pointers

    NULL Pointers Any pointer variable may be given and tested for a value of NULL.

    Important: NULL is not necessarily equivalent to 0.

    void *ptr = NULL; . . . if ( ptr != NULL ) printf( "%d\n", *(int *)ptr ); else printf( "eh?" );

    NULL pointers

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...20Work/AdvancedC/Binders/020Review/220NullPointers.html [3/29/2008 10:01:03 AM]

  • Pointer Operations

    Pointer Operations Non-void pointer variables may be incremented and decremented by an integer value:

    int iarr[] = { 10, 20, 30, 40 }; int inx = 0; int *iptr = iarr;

    for ( inx = 0 ; inx < 2 ; ++inx, iptr += 2 ) printf( "%d ", *iptr );

    Pointer Operations

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...k/AdvancedC/Binders/020Review/230PointerOperations.html [3/29/2008 10:01:03 AM]

  • Pointer Operations

    Pointer Operations Non-void pointer variables pointer to the same array (or the first address after the array) may be compared:

    int iarr[] = { 10, 20, 30, 40 }; int *end = iarr + 4; int *iptr = iarr;

    for ( iptr = iarr ; iptr < end ; ++iptr ) printf( "%d ", *iptr );

    (alternatively)

    for ( iptr = iarr ; iptr < &iarr[4] ; ++iptr ) printf( "%d ", *iptr );

    or subtracted:

    int iarr[] = { 10, 20, 30, 40 }; int *ptr1 = iarr + 1; int *ptr2 = iarr + 3; ptrdiff_t diff = ptr2 - ptr1;

    for ( iptr = iarr ; iptr < end ; ++iptr ) printf( "%d ", *iptr );

    Pointer Operations

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...k/AdvancedC/Binders/020Review/240PointerOperations.html [3/29/2008 10:01:04 AM]

  • Pointer Arithmetic

    Pointer Arithmetic int iarr[] = { 10, 20, 30, 40, 50, 60, 70 }; int *iptr = iarr;

    iptr points to iarr[0] (&iarr[0])

    *iptr refers to the contents of iarr[0] (10)

    (iptr + 1) points to iarr[1] (&iarr[1])

    *(iptr + 1) refers to the contents of iarr[1] (20)

    (iptr + 3) points to iarr[3] (&iarr[3])

    *(iptr + 3) refers to the contents of iarr[3] (40)

    Pointer Arithmetic

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...k/AdvancedC/Binders/020Review/250PointerArithmetic.html [3/29/2008 10:01:04 AM]

  • Pointer Arithmetic

    Pointer Arithmetic int iarr[] = { 10, 20, 30, 40, 50, 60, 70 }; int *iptr = &iarr[3];

    iptr points to iarr[3]

    iptr + 2 points to iarr[5]

    iptr - 2 points to iarr[1]

    iptr = iptr + 1 iptr points to iarr[4];

    iptr += 1 iptr points to iarr[5];

    ++iptr iptr points to iarr[6];

    iptr++ iptr points to iarr[7];

    inx = *ptr++ inx = *ptr then increment ptr

    inx = *++ptr increment ptr then inx = *ptr

    inx = *ptr-- inx = *ptr then decrement ptr

    inx = *--ptr decrement ptr then inx = *ptr

    Pointer Arithmetic

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My%...k/AdvancedC/Binders/020Review/260PointerArithmetic.html [3/29/2008 10:01:04 AM]

  • Strings

    Strings ● Strings are not intrinsic in C.

    ● A string is an array of characters the last of which is the nul

    character:

    char str1[] = { 's', 'p', 'o', 't', '\000' }; char str2[] = "spot"; char str3[5];

    str3[0] = 's'; str3[1] = 'p'; str3[2] = 'o'; str3[3] = 't'; str3[4] = '\000';

    ● The nul character is logically different from the NULL pointer.

    Strings

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My...ool%20Work/AdvancedC/Binders/020Review/310Strings.html [3/29/2008 10:01:04 AM]

  • Strings

    String Literals A string literal (or string constant) is an unnamed character array with an address and a length:

    /* COPIES the 6 element array "sally" into the * 6 element array str1 */ char str1[] = "sally";

    /* Puts the ADDRESS of the 5 element array * "jane" into the variable str2 */ char *str2 = "jane";

    /* Changes the array str1 from sally to tally; * does NOT change the literal "sally" */ str1[0] = 't';

    /*Equivalent to str1[0] = 't' */ *str1 = 't';

    /* UNDEFINED BEHAVIOR; * Attempts to change the literal "jane" to "mane" */ *str2 = 'm';

    Strings

    file:///C|/Documents%20and%20Settings/Jack%20Straub/My...ool%20Work/AdvancedC/Binders/020Review/320Strings.html [3/29/2008 10:01:05 AM]

  • Strings

    String Operations Since

Recommended

View more >