Professional Documents
Culture Documents
07 Pointers Dist
07 Pointers Dist
1
1. Define a pointer
2. Differentiate a pointer variable and a variable of a
Topic primitive type
2
A pointer is a variable that contains the address of a variable
Operators involved
& - gives the address of a variable
* - indirection or dereferencing operator
3.14 88
6F93 A100
’C’
4
#include <stdio.h>
A100 14
void main() {
px x
int x, *px;
6F93 A100
int* p1, p2, p3;
x = 14;
Sample Code px = &x;
Use C Visualization p1 = px; A100
p2 = p1; p1 = px;
p3 = p2; p1
44C4
printf("value of x = %d", x);
printf("\naddress of x = %p", px);
}
Try this out yourself
WARNING! Issue in C Visualization
What is p2 and p3? 5
int x, *px, y, z;
int* p1;
x = 14;
px = &x;
p1 = px;
6
int x, *px, y, z;
int* p1;
x = 14; A100 14 *px
px = &x;
p1 = px; px x
7
Create a function that would swap the contents of two variables
Case Study
Swap Function
Pass-by-copy //The values 2 and 5 being passed are actual parameters
Case Study
Swap Function
Pass-by-address
10
End of Lecture
Next Meeting: Pointer problem examples
Practice coding + explore double pointers and beyond
11
Function compute() will accept as parameters two values A and B. If A is
greater than B, the function will multiply A and B, and put the product in A;
otherwise, the function will put the sum of A and B in A.
What should be the function header?
Review Write a sample function call with necessary declaration of all
on variables used in the call. Initialize them if necessary.
Your Own he fu
nction.
f t
code o
te the
Wri
12