You are on page 1of 12

Pointers

with Sir Beets

1
1. Define a pointer
2. Differentiate a pointer variable and a variable of a
Topic primitive type

Objectives 3. Use pointer operations in a simple C function


4. Write statements accessing the variables pointed to by the
pointer variable

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

Pointer vs Primitive (Scalar)


Pointers – Pointers contain the address of a variable (hex/octal/bin)
– Scalars contain character or numeric values
Variables are holders of values

3.14 88
6F93 A100
’C’

P_Pointer Pointer Scalar


Variable
44C4 6F93 A100
address
3
#include <stdio.h>
px = &x; x = 14;
void main() {
A100 14
int x;
px x
int* px;
Sample Code x = 14; 6F93 A100
Use C Visualization px = &x;

printf("value of x = %d", x);


printf("\naddress of x = %p", px);

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;

Sample Code y = *px;

Use C Visualization z = *p1;

printf("value of x = %d", x);


printf("\naddress of x = %p", px); Using * in a pointer is called
printf("\nvalue of y = %d", y); dereferencing which talks about the
action of accessing a value in a
printf("\nvalue of z = %d", z); memory location.

6
int x, *px, y, z;
int* p1;
x = 14; A100 14 *px
px = &x;
p1 = px; px x

Sample Code y = *px; 6F93 A100 px


Use C Visualization z = *p1;

printf("value of x = %d", x);


printf("\naddress of x = %p", px); Using * in a pointer is called
printf("\nvalue of y = %d", y); dereferencing which talks about the
action of accessing a value in a
printf("\nvalue of z = %d", z); memory location.

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

/* variables a and b are


formal parameters */

What should be changed so that the


Values towards the
actual parameters are the variables
themselves and not just the values? end of swap function
8
Create a function that would swap the contents of two variables

Case Study
Swap Function
Pass-by-address

Values towards the


end of swap function
9
If px points to the integer x, defined by px = &x
then *px can occur in any context to represent x,
so *px = *px+1; is the same as x = x+1;
Unary operators * and & bind more tightly than arithmetic
operators,
Pointers
*px += 1; /* increments what px points to */
So does,
++*px; and (*px)++;
Parenthesis are necessary because without them, the expression
would increment px instead of what it points to.

Try this out yourself

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

You might also like