Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Pointers

In C we allocate an area of memory for storing our variable constants, this is done
when we declare a variable.
When we do so we define the variable type and a label which represents the value to
which it is assigned
The variable on run time will be allocated a space in memory which is represented by
our variable name e.g.

MEMORY
val1
int val1;

© G Charalambous 1
Pointers

The compiler will on compilation carry out syntax analysis this is simply a validation of the
syntax such that it is grammatically correct.
At this stage a dictionary is created, this is a list of the all the variable labels identifying
their types and a place in memory for them.
For a given variable when encountered the compiler will use the dictionary to identify its
given location and type – size in memory

C also allows the use of pointers these are simply variable labels which instead of storing a
value will store an address for a given type of variable

© G Charalambous 2
Pointers
In C its occasionally useful to refer to given variable by its address rather than its name,
we do this by placing the & sign before the variable label
So &variable_label refers to the address of variable_label

We have seen its use when using scanf, we read a given value from the keyboard and
place this at a given address
e.g.

scanf(“%d”,&var1);

Here we read an integer from the keyboard and place it at the address
of var1

© G Charalambous 3
Pointer variables

Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ;

This defines the type of variable the pointer will point to

© G Charalambous 4
Pointer variables
Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ;

The * character in the variable declaration indicates that the variable


being declared will be suitable for holding addresses
© G Charalambous 5
Pointer variables
Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ;

This is the variable name for our pointer, where the label represents not a
value but an address
© G Charalambous 6
Pointer variables
Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ; e.g int *int_pointer;

This defines what type of value


we are going to point to

© G Charalambous 7
Pointer variables
Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ; e.g int *int_pointer;

So here our pointer will point to memory


Pointer variables location which will hold an integer – two bytes
in length
© G Charalambous 8
Pointer variables
Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ; e.g int *int_pointer;

This defines that we are declaring a pointer


variable – i.e a variable that can hold
addresses
© G Charalambous 9
Pointer variables
Instead of dealing with variables just as a direct representation of a given value we can
deal with their addresses as well.
A pointer is a variable label which instead of representing a value, it represents an
address for a given data type

To declare a pointer

type * label ; e.g int *int_pointer;

This is the pointer label

© G Charalambous 10
Declaration of Pointer
So in memory our declaration we have

int *int_pointer; Memory

int_pointer

In our declaration we have defined 0000


a variable which when initialised
will hold an address in memory to
point to a variable of type integer

The pointer here has not


been initialised

© G Charalambous 11
Initialising pointers
Having declared a pointer we need to allocate an address to it so that it can point to a
given location in memory.

To initialise this has the following form:-

Pointer_label = &variable;

© G Charalambous 12
Example

Consider the following code:-

#include <stdio.h>
main() { We declare a pointer which will
point to an address containing
char *ptr1_test; a single character
char test;
ptr1_test = &test;
return 0;
}

© G Charalambous 13
Example

Consider the following code:-

#include <stdio.h>
main() {
char *ptr1_test; We declare a variable of type
char test; char with label test
ptr1_test = &test;
return 0;
}

© G Charalambous 14
Example

Consider the following code:-

#include <stdio.h>
main() {
char *ptr1_test;
char test; We copy the address of the
variable test into our pointer
ptr1_test = &test;
return 0;
}

© G Charalambous 15
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


char test;
ptr1_test = &test;
return 0;
}

We declare our pointer which is


allocated in memory at address
1A1E

© G Charalambous 16
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


char test;
ptr1_test = &test;
return 0;
}

So the compiler has in the


dictionary allocated for the
variable ptr1_test the address
1A1E

© G Charalambous 17
Consider the following code:-
MEMORY
#include <stdio.h>
main() { This represents the
1A1E address of the
pointer
char *ptr1_test; 0000
char test;
ptr1_test = &test;
return 0;
}

This represents the contents


of the pointer

The value stored at 1A1E is


unimportant as the pointer has
not been assigned any address

© G Charalambous 18
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


char test;
ptr1_test = &test;
return 0; 1B2D

We declare our variable test


which is allocated in memory at
address 1B2D

© G Charalambous 19
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


char test;
ptr1_test = &test;
return 0; 1B2D

The compiler has allocated a


single byte (of type char) which is
uninitialised

© G Charalambous 20
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


char test;
ptr1_test = &test; This represents the
return 0; 1B2D address of the
variable test
}

This represents the


The compiler has in the dictionary contents of the variable
allocated for the variable test the test
address 1B2D

© G Charalambous 21
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


1B2D
char test;
ptr1_test = &test;
return 0; 1B2D

Copy the address of the variable


test into the pointer ptr1_test

© G Charalambous 22
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E

char *ptr1_test; 0000


1B2D
char test;
ptr1_test = &test;
return 0; 1B2D

Now the pointer ptr1_test


contains the address of test
and so symbolically we say
that the pointer ptr1_test
points to the variable test © G Charalambous 23
Assigning a value to an address that a pointer
points to
Let us extend the program but now show we can assign values using
pointers

#include <stdio.h> MEMORY


main() {

char *ptr1_test; 1A1E


char test;
ptr1_test = &test;
test = 'C';
*ptr1_test = 'B'; We assign to the
1B2D
variable labelled test
return 0; C the ASCII value of C

© G Charalambous 24
Assigning a value to an address that a pointer
points to
Let us extend the program but now show we can assign values using
pointers

#include <stdio.h> MEMORY


main() {

char *ptr1_test; 1A1E


char test;
1B2D
ptr1_test = &test;
test = 'C';
*ptr1_test = 'B'; 1B2D
We assign the ASCII
value of B to the
return 0; B
C address which is held
by pointer ptr1_test
}

© G Charalambous 25
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {

char *ptr1, *ptr2;


char test;
ptr1 = &test; Declare two pointers
ptr2 = ptr1; ptr1(- at address 1A1E)
test = 'C'; ptr2(- at address 1A2E)
*ptr2 = 'B'; Both can hold addresses that will
return 0; point variables of type char – 1 byte
}

. © G Charalambous 26
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {

char *ptr1, *ptr2;


1A30
char test;

ptr1 = &test;
ptr2 = ptr1;
test = 'C';
Declare variable test of type char – 1
*ptr2 = 'B'; byte
return 0; This is allocated at address 1A30

. © G Charalambous 27
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {
1A30

char *ptr1, *ptr2;


1A30
char test;

ptr1 = &test;
ptr2 = ptr1;
test = 'C'; Copy the address of variable test into
*ptr2 = 'B'; ptr1
return 0;
}

. © G Charalambous 28
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {
1A30 1A30

char *ptr1, *ptr2;


1A30
char test;

ptr1 = &test;
ptr2 = ptr1;
test = 'C'; Copy the contents of ptr1 into ptr2
*ptr2 = 'B';
return 0;
}

© G Charalambous 29
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {
1A30 1A30

char *ptr1, *ptr2;


1A30
char test;

ptr1 = &test;
ptr2 = ptr1; Both ptr1 and ptr2 contain the same
test = 'C'; address so they both point to the
variable test which has the address
*ptr2 = 'B'; 1A30
return 0;
}

© G Charalambous 30
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {
1A30 1A30

char *ptr1, *ptr2;


1A30
char test; C
ptr1 = &test;
ptr2 = ptr1;
test = 'C'; Allocate the ASCII value of C to the
*ptr2 = 'B'; variable test
return 0;
}

© G Charalambous 31
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY

#include <stdio.h> 1A1E 1A2E


main() {
1A30 1A30

char *ptr1, *ptr2;


1A30
char test; B
C
ptr1 = &test;
ptr2 = ptr1;
test = 'C'; Allocate the ASCII value of B to the
*ptr2 = 'B'; address that ptr2 contains (or is
pointing to, i.e 1A30)
return 0;
}

© G Charalambous 32
Summary
To declare a pointer that will point to a variable of a given type
type *pointer_label;
To initialise a pointer
1. Copy the address of a variable
Pointer_label = &variable;
2. Copy the value held by another pointer
Pointer_label_1 = Pointer_label_2;
(Note Pointer_label_2 needs to contain an address
To allocate a value at the address held by a pointer
*pointer_label = value; © G Charalambous 33

You might also like