600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general and you should not follow my lead on this.
*ptr = *ptr++; In terms of precedence: *ptr = *(ptr++); ptr++ increments the pointer but returns the old value, so the RHS side becomes This gives *(ptr++) ra[0]; ptr ra+1 ra[1]=ra[0]; int ra[5] = 1, 2, 3, 4, 5 ; int *ptr = ra; for( int i=0 ; i<5 ; i++ ) *ptr = *ptr++; printf( "array is: [ ); for( int i=0 ; i<5 ; i++ ) printf( "%d ", ra[i] ); printf( "]\n ); >>./a.out array is: [ 1 1 1 1 1 ] >>
In addition to *ptr giving = *ptr++; the wrong result, In terms this is of bad precedence: code because in the last iteration *ptr we = *(ptr++); are setting ptr++ ra[5] increments = ra[4]; the pointer which but is returns an out-of-bounds the old value, access! so the RHS side becomes *(ptr++) ra[0]; ptr ra+1 This gives ra[1]=ra[0]; int ra[5] = 1, 2, 3, 4, 5 ; int *ptr = ra; for( int i=0 ; i<5 ; i++ ) *ptr = *ptr++; printf( "array is: [ ); for( int i=0 ; i<5 ; i++ ) printf( "%d ", ra[i] ); printf( "]\n ); >>./a.out array is: [ 1 1 1 1 1 ] >>
with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, ); >>./a.out >>
with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, );
with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, ); list1 list2
with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, ); list1 list2
with copies of the pointers and Swapping the addresses stored in list1 and list2 happens with the stack frame of swap and is not visible when the function returns void swap( char * list1, char * list2 ) char * temp = list1; list1 = list2; list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap(, );
with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &); >>./a.out >>
with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &);
with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &); list1 list2
with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &); list1 list2
with the addresses of the pointers and Swapping the contents of data pointed to by list1 and list2 changes what and point to The effects are visible even after the function returns void swap( char ** list1, char ** list2 ) char * temp = *list1; *list1 = *list2; *list2 = temp; char ar1[] = 'a', 'b', 'c', 'd', 'e' ; char ar2[] = 'f', 'g', 'h', 'i', 'j' ; swap( &, &);
Piazza Resources section Resources tab Exercise 5-1