Pointers and Reference Parameters

Pointers

Pointers are variable that store memory address

Before we leadn about pointers, we must lean more about addresses

AddressVariableValue
0x7fff380bab4ca181
0x7fff380bab50pa0x7fff380bab4c
Pointers and Reference Parameters

Computer memory

ram computer

Memory is just long list of numbers one after the other

Ram 8 gb has about 233 of these number

Each number is 8 bits (byte)

We combine them to make integers and floating point values

Pointers and Reference Parameters

Memory Address

Memory addresses in computers are often 32 bits (or nowadays, 64 bits)

0111111111111111111110101000110

Another way to represent an address is to use hexadecimal:

0x7ffffa8c

Pointers and Reference Parameters

Pointers

Pointers are variables that contain address

Just like other variable, they must be declared before beging used

Declaration:

int *p /*Instead of int p for integers*/

int* mean p is a pointer variable that stores the address of an integer variable

Pointers and Reference Parameters

Pointer Initialization

Declaration:

int a = 2; // a is an integer
int *pa = &a; //pa is a pointer containing the address of a
			

"&" operator means "address of" read it as "at"

Pointers and Reference Parameters

Example Program

int a = 21;
int *p = &a;
printf("%d\n", a); //21
printf("%p\n", &a); //0x7fff5b8cd88c
printf("%p\n", p); //0x7fff5b8cd88c 
printf("%d\n", *p); //21
printf("%p\n", &p); //0x7fff5b8cd890
				

Operators:

& "addressof"

* "dereference"

Pointers and Reference Parameters

Example Pointers

int a = 15, b = 38;
int *c = &a; // int *c; c = &a;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
a = 49;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
c = &b;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
			
Pointers and Reference Parameters

Example Pointers


int a = 15, b = 38;
int *c = &a; // int *c; c = &a;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
a = 49;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
c = &b;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
			

First Section

Declares a and b as integers

Declares c as a pointer that contains the adddress of a ("poits to a")

AddressMemoryName
0x7fff11a949f815a
0x7fff11a949fc38b
0x7fff11a94a000x7fff11a949f8c
Note the difference between *c in variable declaration and *c in printf
Pointers and Reference Parameters

Example Pointers

int a = 15, b = 38;
int *c = &a; // int *c; c = &a;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);

a = 49;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
c = &b;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
			

Seccond Section

AddressMemoryName
0x7fff11a949f81549a
0x7fff11a949fc38b
0x7fff11a94a000x7fff11a949f8c
Pointers and Reference Parameters

Example Pointers

int a = 15, b = 38;
int *c = &a; // int *c; c = &a;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
a = 49;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);

c = &b;
printf("%p %d\n", &a, a);
printf("%p %d\n", &b, b);
printf("%p %d %d\n", &c, c, *c);
			

Third Section

AddressMemoryName
0x7fff11a949f849a
0x7fff11a949fc38b
0x7fff11a94a000x7fff11a949fcc
Pointers and Reference Parameters

Reference parameters

A valuable use for pointers: Passing addresses to a function

Pointers and Reference Parameters

Argument & Returned Value

Condider a function call y = f(x)

The value x is passed to the function f

A value is returned and assigned to y

By passed we mean that the value of argument x is copied to the parameter in the function. Some calculation is performed and the result is returned and assign to y

Pointers and Reference Parameters

Example

int square(int t);
void main(){
	int x, y;
	x = 5;
	y = square(x);
}
int square(int t){
	return t * t;
}
				
AddressMemoryName
0x7fff11a949f8...x
0x7fff11a949fc...y
...
...
Pointers and Reference Parameters

Example

int square(int t);
void main(){
	int x, y;
	x = 5;
	y = square(x);
}
int square(int t){
	return t * t;
}
				
AddressMemoryName
0x7fff11a949f85x
0x7fff11a949fc...y
...
...
Pointers and Reference Parameters

Example

int square(int t);
void main(){
	int x, y;
	x = 5;
	y = square(x);
}
int square(int t){
	return t * t;
}
				

The call square(x):

creates a variable t

copies the value of x to t

AddressMemoryName
0x7fff11a949f85x
0x7fff11a949fc...y
...
0x7fff11a94a005t
Pointers and Reference Parameters

Example

int square(int t);
void main(){
	int x, y;
	x = 5;
	y = square(x);
}
int square(int t){
	return t * t;
}
				

The call square(x):

creates a variable t

copies the value of x to t

calculates t * t

returns temp

AddressMemoryName
0x7fff11a949f85x
0x7fff11a949fc...y
...
0x7fff11a94a005t
0x7ffe6734f85c25temp
Pointers and Reference Parameters

y = f(x)

Only one valued returned

What if we want to return more than one value?

Solution is to use pointers to variables in the calling function

Pointers and Reference Parameters

How to do this in C

The approach is to pass to the address (using the & operator) of the value to be modified.

We call such as parameter a reference parameter.

Use the * operator to change the reference parameter value

Pointers and Reference Parameters

Function Reference Params

int val = 10;
calculate(&val);
printf("%d\n", val);
void calculate(int *param){
	*param = 27;
}
			

What will this do different?

int val = 10;
calculate(val);
printf("%d\n", val);
void calcualate(int param){
	param = 27;
}