Professional Documents
Culture Documents
Pointers in C
Pointers in C
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 ;
© 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 ;
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
© 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
To declare a pointer
To declare a pointer
© G Charalambous 10
Declaration of Pointer
So in memory our declaration we have
int_pointer
© 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.
Pointer_label = &variable;
© G Charalambous 12
Example
#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
#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
#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
© G Charalambous 16
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
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;
}
© G Charalambous 18
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E
© G Charalambous 19
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E
© G Charalambous 20
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E
© G Charalambous 21
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E
© G Charalambous 22
Consider the following code:-
MEMORY
#include <stdio.h>
main() {
1A1E
© 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
© G Charalambous 25
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY
. © G Charalambous 26
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY
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
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
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
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
© G Charalambous 31
We can assign copy the address held by a pointer into another pointer of
the same type
Consider the following:-
MEMORY
© 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