Docsity

Analysis of Call-by-Value and Call-by-Reference in C with Pointers and Arrays, Slides of Computer Fundamentals

Toggle sidebar
bg1
int
main()
{
int
number = 5
;
number=cubeByValue(number);
}
int
cubeByValue( int n )
{
return
n * n * n;
}
number
5
n
Before main
calls
cubeByValue :
undefined
int main()
{
int number = 5 ;
number = cubeByValue( number );
}
int cubeByValue( int n )
{
return n * n * n;
}
number
5
n
After cubeByValue
receives the call:
5
125
int cubeByValue( int
n )
{
return n * n * n;
}
int main()
{
int number = 5 ;
number = cubeByValue( number );
}
number
5
n
After cubeByValue
cubes parameter n and before cubeByValue returns to main
:
5
Fig. 7.8 Analysis of a typical call-by-value. (Part 1 of 2.)
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Analysis of Call-by-Value and Call-by-Reference in C with Pointers and Arrays and more Slides Computer Fundamentals in PDF only on Docsity!

int main() {

int number = 5 ;

number=cubeByValue(number); } int (^) cubeByValue( int n ) { return n * n * n; } number 5 n Before main^ calls cubeByValue^ : undefined int main() {

int number = 5 ;

number = cubeByValue( number );

int cubeByValue( int n ) { return n * n * n; } number 5 n After cubeByValue^ receives the call:

int cubeByValue( int n )

return n * n * n; } int main() {

int number = 5 ;

number = cubeByValue( number ); } number

n After cubeByValue cubes parameter n and before cubeByValue returns to main :

Fig. 7.8 Analysis of a typical call-by-value. (Part 1 of 2.)

int main() {

int number = 5 ;

number = cubeByValue( number ); }

int cubeByValue( int n )

return n * n * n; } number

n After cubeByValue^ returns to main^ and before assigning the result to number: undefined 125 125 int main() {

int number = 5 ;

number = cubeByValue( number ); } int cubeByValue( int n ) { return n * n * n; } number

n After main^ completes the assignment to number: undefined Fig. 7.8 Analysis of a typical call-by-value. (Part 2 of 2.)

Swap

temp = x ;

x = y ;

y = temp ;

Example

void swap ( int * , int * );

void main ( )

int x = 10 , y = 20 ;

cout << x << endl << y << endl;

swap ( &x , &y ) ;

cout << x << endl << y;

Pointers and Arrays

The name of the array is like a constant pointer which contains the address of the first element. Pointers and Arrays

Declaration of a Pointer Variable int y [ 10 ] ; int *yptr ; yptr = y ; yptr is a pointer to integer The array name (y) is actually the address of first element of the array y[ 5 ] yPtr = &y[ 0 ] Explicitly assigns yPtr to address of first element of y

Pointer Expressions and Pointer Arithmetic  Arithmetic operations can be performed on pointers

  • Increment/decrement pointer (++ or --)
  • Add an integer to a pointer( + or += , - or -=)
  • Pointers may be subtracted from each other
  • Operations meaningless unless performed on an array

 5 element int array on machine with 4 byte ints

  • yPtr points to first element y[ 0 ]  at location 3000 (yPtr = 3000)
  • yPtr += 2; sets yPtr to 3008  yPtr points to y[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to address 3008 Pointer Expressions and Pointer Arithmetic

pointer variable yPtr y[0] y[1] y[2] y[3] y[4] 3000 3004 3008 3012 3016 location

Pointer Expressions and Pointer Arithmetic  Subtracting pointers

  • Returns number of elements from one to the other. If vPtr2 = v[ 2 ]; vPtr = v[ 0 ];
  • vPtr2 - vPtr would produce 2  Pointer comparison ( <, == , > )
  • See which pointer points to the higher numbered array element
  • Also, see if a pointer points to 0

The Relationship Between Pointers and Arrays

  • Element b[ 3 ]  Can be accessed by *( bPtr + 3 ) - Where n is the offset. Called pointer/offset notation  Can be accessed by bptr[ 3 ] - Called pointer/subscript notation - bPtr[ 3 ] same as b[ 3 ]  Can be accessed by performing pointer arithmetic on the array itself *( b + 3 )