Download Pointer Arithmetic and Array Manipulation in C and more Study notes Computer Science in PDF only on Docsity!
Call by value
Function arguments in C are passed by value
The value of the argument is passed, not a reference
Functions are given a new copy of their arguments
So a function can’t modify the value of a variable in the
calling function (unless you use pointers)
int F( int a ) { a=0; return a; } void main() { int A = 1, B; B = F(A); printf( "%d %d\n”, A, B ); // prints “1 0” }
Call by value
Example
void swap ( int a, int b ) { int temp = a; a = b; b = temp; }
void main() { int A = 1, B = 2; swap(A, B); printf( "%d %d\n”, A, B ); }
Recursion
Recursion is a powerful technique for
specifying sets, functions, and programs
A recursive function is a function that refers to
itself
Many algorithms can be written more compactly,
easily, elegantly using recursion
Early languages (e.g. Fortran) did not support
recursion
C and most other modern languages do
Recursion is possible because of the execution
stack
Recursion example
Example: computing positive integral powers
an^ = a·a·a···a (n times)
Recursive definition:
a^0 = 1, an+1^ = a·an
int power(int base, int exp) { if(exp == 0) return 1; else return base*power(base, exp-1); }
int main(void) { printf(“%d\n”, power(5, 2)); return 0; }
Recall: C is call by value
Modifying the value of a parameter in a
function does not modify variables in the
calling function.
Recall: C is call by value
void foo(int x) { x = 5; }
int main(void) { int y = 10; foo(y); printf(“%d\n”, y); // prints 10, not 5 return 0; }
Call by value
Example
void swap ( int a, int b ) { int temp = a; a = b; b = temp; }
void main() { int A = 1, B = 2; swap(A, B); printf( "%d %d\n”, A, B ); }
4 Rules for pointers
int *x,y;
Declaration
*x = ... ;
Assigns value in location x
(... = ) *x;
Takes value in location x
(... =) &y;
Take address
Declaring pointer variables
A pointer variable stores a memory address
Use a * to declare a pointer variable
e.g.:
int *p1; // declare a variable called p1 that // stores a pointer to an integer
char *p2; // declare a variable called p2 that stores
// a pointer to a character
Pointers: a first example
int main( void )
int x = 3, z;
// create a pointer variable, and
// set it to the address of x
int *ptr = &x;
// set z to the value pointed to
// by ptr
z = *ptr;
// Changes value at ptr
*ptr = 4;
x
z
ptr
Don’t get confused!
Pointer notation is confusing, especially at first
*’s are used in two ways: in declarations, and as
an operator
In declarations, * creates a pointer variable,
e.g.
int *ptr; declares a pointer to variables of type int
As an operator, * dereferences a pointer, e.g.
*ptr returns the value stored in memory at the
address contained in ptr
Passing pointers to functions
Recall that foo changes local variable x, but
not the original variable y
void foo(int x) { x = 5; }
int main(void) { int y = 10; foo(y); printf(“%d\n”, y); // prints 10, not 5 return 0; }
Passing pointers to functions
This version does modify y
void foo(int *x) { *x = 5; }
int main(void) { int y = 10; foo(&y); printf(“%d\n”, y); // prints 10, not 5 return 0; }
Another example: swap
This swap() function works as expected
void swap ( int *a, int *b ) { int temp = *a; *a = *b; *b = temp; }
void main() { int A = 1, B = 2; swap(&A, &B); printf( "%d %d\n”, A, B ); // prints “2 1” }
scanf
scanf uses this technique to modify its
parameters
What happens if you forget the &?
int I; printf(“enter an integer: “); scanf(“%d”, &I);
passes pointer to I to scanf