0% found this document useful (0 votes)
16 views35 pages

03 Pointers

The document provides an introduction to pointers in programming, explaining their purpose, how to declare them, and their relationship with memory addresses. It covers the use of the & operator to obtain addresses, the * operator for dereferencing, and demonstrates how pointers can be used as function parameters to modify variable values. Additionally, it includes examples of both ineffective and effective ways to swap values using pointers.

Uploaded by

maxxedd6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views35 pages

03 Pointers

The document provides an introduction to pointers in programming, explaining their purpose, how to declare them, and their relationship with memory addresses. It covers the use of the & operator to obtain addresses, the * operator for dereferencing, and demonstrates how pointers can be used as function parameters to modify variable values. Additionally, it includes examples of both ineffective and effective ways to swap values using pointers.

Uploaded by

maxxedd6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 35

ICT 107

Pointers

1
Topics
• Introduction to pointers
• Pointers and function parameters

2
Memory Address of a Variable
char ch = ’A’;

ch:
0x2000 ’A’
The memory
The value of the
address of the
variable ch
variable ch 3
The & Operator
• Gives the memory address of an object

char ch = ’A’;

0x2000 ’A’
&ch yields the value 0x2000

• Also known as the “address operator”


4
Example:

char ch;
printf(“%p”, &ch);

“conversion specifier” for


printing a memory address

5
Pointers
A variable which can store
the memory address of
another variable
0x3A15

0x2000
chPtr

0x1FFE 0x1FFF 0x2000 0x2001 0x2002


etc
‘B’
ch 6
Pointers
• A pointer is a variable
• Contains a memory address
• Points to a specific data type
• Pointer variables are usually named varPtr

7
Example:
char* cPtr;
cPtr:
0x2004

Can store an address of


variables of type char

• We say cPtr is a pointer to char


8
Pointers and the & Operator
Example:
char c = ’A’;
char *cPtr;

cPtr = &c; Assigns the


address of c to cPtr

c: cPtr:

A 0x2000
0x2000 0x2004 9
Notes on Pointers
• We can have pointers to any data type
Example: int* numPtr;
float* xPtr;

• The * can be anywhere between the


type and the variable
Example: int *numPtr;
float * xPtr;
10
Notes on Pointers (cont)
• You can assign the address of a variable to a
“compatible” pointer using the & operator
int aNumber;
int *numPtr;
Example:

numPtr = &aNumber;

• You can print the address stored in a pointer


using the %p conversion specifier
Example: printf(“%p”, numPtr); 11
Notes on Pointers (cont)

Beware of pointers
int *numPtr; which are not
initialized!

???
numPtr

12
Notes on Pointers (cont)

• When declaring a pointer, it is a good


idea to always initialize it to NULL
(a special pointer constant)

int *numPtr = NULL;

NULL
numPtr

13
The * Operator
• Allows pointers to access variables they
point to
• Also known as “dereferencing operator”
• Should not be confused with the * in the
pointer declaration

14
Pointers and the  Operator
Example: char c = ’A’;
char *cPtr = NULL;

Changes the value of


cPtr = &c; the variable which cPtr
*cPtr = ’B’; points to

c: cPtr:

B
A 0x2000
NULL
0x2000 0x2004 15
Easy Steps to Pointers
• Step 1: Declare the variable to be pointed to
int num;
char ch = ‘A’;
float x;

num:
ch: ‘A’
x:
16
Easy Steps to Pointers (cont)
• Step 2: Declare the pointer variable
int num;
char ch = ‘A’; numPtr: NULL
float x; chPtr: NULL
int* numPtr = NULL; xPtr: NULL
char *chPtr = NULL;
float * xPtr = NULL;
num:
ch: ‘A’
x:
17
Easy Steps to Pointers (cont)
• Step 3: Assign address of variable to pointer
int num; numPtr: addr of num
char ch = ‘A’;
float x; chPtr: addr of ch
int* numPtr = NULL;
char *chPtr = NULL; xPtr: addr of x
float * xPtr = NULL;
numPtr = #
chPtr = &ch; num:
xPtr = &x;
ch: ‘A’
x:
A pointer’s type has to correspond to
the type of the variable it points to 18
Easy Steps to Pointers (cont)
• Step 4: De-reference the pointers
int num; addr of num
char ch = ‘A’; numPtr:
float x; addr of ch
chPtr:
int* numPtr = NULL;
char *chPtr = NULL; xPtr: addr of x
float * xPtr = NULL;
numPtr = #
chPtr = &ch; num: 65
xPtr = &x;
ch: ‘A’
*xPtr = 0.25;
*numPtr = *chPtr;
x: 0.25
19
Pointers and Function Parameters
• Example: Function to swap the values of two
variables

x: 1 x: 2
swap
y: 2 y: 1

20
#include <stdio.h> Bad swap

void swap1(int a, int b)


{
int tmp;
tmp = a;
a = b;
b = tmp;
return;
}
int main()
{
int x = 1, y = 2;
swap1(x, y);
printf(“%d %d\n”, x, y);
return 0;
} 21
#include <stdio.h> Bad swap

void swap1(int a, int b)


{
int tmp;
tmp = a;
a = b;
b = tmp;
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap1(x, y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
22
#include <stdio.h> Bad swap

void swap1(int a, int b)


{ tmp:
int tmp;
a: 1
tmp = a;
a = b;
b = tmp; b: 2
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap1(x, y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
23
#include <stdio.h> Bad swap

void swap1(int a, int b)


{ tmp: 1
int tmp;
a: 1
tmp = a;
a = b;
b = tmp; b: 2
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap1(x, y);
printf(“%d %d\n”, x, y); y: 2
return 0;
} 24
#include <stdio.h> Bad swap

void swap1(int a, int b)


{ tmp: 1
int tmp;
a: 2
tmp = a;
a = b;
b = tmp; b: 2
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap1(x, y);
printf(“%d %d\n”, x, y); y: 2
return 0;
} 25
#include <stdio.h> Bad swap

void swap1(int a, int b)


{ tmp: 1
int tmp;
a: 2
tmp = a;
a = b;
b = tmp; b: 1
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap1(x, y);
printf(“%d %d\n”, x, y); y: 2
return 0;
} 26
#include <stdio.h> Bad swap

void swap1(int a, int b)


{ tmp: 1
int tmp;
a: 2
tmp = a;
a = b;
b = tmp; b: 1
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap1(x, y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
27
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
return;
}
int main()
{
int x = 1, y = 2;
swap2(&x, &y);
printf(“%d %d\n”, x, y);
return 0;
} 28
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap2(&x, &y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
29
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{ tmp:
int tmp;
a: addr of x
tmp = *a;
*a = *b;
*b = tmp; b: addr of y
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap2(&x, &y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
30
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{ tmp: 1
int tmp;
a: addr of x
tmp = *a;
*a = *b;
*b = tmp; b: addr of y
return;
}
int main()
{
int x = 1, y = 2; x: 1
swap2(&x, &y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
31
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{ tmp: 1
int tmp;
a: addr of x
tmp = *a;
*a = *b;
*b = tmp; b: addr of y
return;
}
int main()
{
int x = 1, y = 2; x: 2
swap2(&x, &y);
printf(“%d %d\n”, x, y); y: 2
return 0;
}
32
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{ tmp: 1
int tmp;
tmp = *a; a: addr of x
*a = *b;
*b = tmp; b: addr of y
return;
}
int main()
{
int x = 1, y = 2;
x: 2
swap2(&x, &y);
printf(“%d %d\n”, x, y); y: 1
return 0;
}
33
#include <stdio.h> Good swap

void swap2(int* a, int* b)


{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
return;
}
int main()
{
int x = 1, y = 2; x:
2
swap2(&x, &y);
printf(“%d %d\n”, x, y); y:
return 0; 1
}
34
Pointers and Function Arguments
• Change the value of an actual parameter variable

• scanf demystified
char ch;
int numx;
float numy;
scanf(“%c %d %f”, &ch, &numx, &numy);

35

You might also like