CMP1401: Introduction To Programming With C: Ece Gelal Soyak Bahc Es Ehir Universitesi
CMP1401: Introduction To Programming With C: Ece Gelal Soyak Bahc Es Ehir Universitesi
P
Bahçeşehir Üniversitesi
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 1 / 24
Pointers Pointer Arithmetic and Iterators Summary
In this lecture...
0 1
Storing the address for a variable: Pointers
Pointer arithmetic
10
Pointers vs arrays
P
M
Using pointers to iterate within an array
C
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 2 / 24
Pointers Pointer Arithmetic and Iterators Summary
0 1
10
A pointer that stores the address of variable x points to x
The value of x can be accessed through “dereferencing”
0x7fff53b90a38
P ptr
M
0x7fff53b90a3c 0x7fff53b90a44
0x7fff53b90a40
C
0x7fff53b90a44 x
0x7fff53b90a48
.. ..
. .
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 3 / 24
Pointers Pointer Arithmetic and Iterators Summary
Declaring Pointers
0 1
Pointer to a data type E is denoted by E*
E.g. int*, double*, char*
10
int * ptr;
P
// declares a pointer to an integer variable
M
ptr is the address of the integer
C
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 4 / 24
Pointers Pointer Arithmetic and Iterators Summary
0 1
10
In C you get the address of a variable with the & operator
MEMORY:
P
int foo;
0x7fff53b90a38
int * x;
0x7fff53b90a3c
M
foo = 13;
x = &foo; // ‘‘the address of foo’’ 0x7fff53b90a40
printf("%p", x); 13
C
0x7fff53b90a44
0x7fff53b90a48
Outputs: 0x7fff53b90a44
0x7fff53b90a4c
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 5 / 24
Pointers Pointer Arithmetic and Iterators Summary
Pointers Review
Example:
int x, y;
0 1
0
int ∗ ptr ;
x = 320; // Step 1
1
ptr = &x; // Step 2
y = ∗ ptr ; // Step 3
Step 1:
ptr
Step 2:
P ptr
Step 3:
M
0x0a38 0x0a38 0x0a40 0x0a38 0x0a40 ptr
0x0a3c 0x0a3c 0x0a3c
C
0x0a40 320 x 0x0a40 320 x 0x0a40 320 x
0x0a44 y 0x0a44 y 0x0a44 320 y
0x0a48 0x0a48 0x0a48
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 6 / 24
Pointers Pointer Arithmetic and Iterators Summary
Initializing Pointers
0 1
0
To initialize a pointer variable ptr to point to an integer variable x:
1
int ∗ ptr = &x;
P
int ∗ ptr declares the pointer to an integer value, which we are
initializing to the address of x
C M
ptr is a pointer to an integer value
ptr has value of the address of x
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 7 / 24
Pointers Pointer Arithmetic and Iterators Summary
0 1
0
* is placed before the identifier to indicate the identifier is a pointer type
1
int x; // x is an integer
int ∗ x; // x is a pointer to an integer
int y = 5;
int ∗ x = &y;
C
printf ( ”%d\n”, ∗x ); // reads the value 5 pointed to by x
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 8 / 24
Pointers Pointer Arithmetic and Iterators Summary
Dereferencing Pointers
10
P
if p is pointer, ∗p is the object to which p refers
*p = 3;
int j = *p;
C M
side of an assignment; e.g.,
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 9 / 24
Pointers Pointer Arithmetic and Iterators Summary
Dereferencing Example
#include <stdio.h>
0 1
int main()
{
int i , ∗p = &i;
10 Outputs:
P
printf ( ”p = %p\n”, p); p = 0x7fff56719b4c
i = 4;
i=4
printf ( ” i = %d\n”, i ) ;
M
∗p = 10; i = 10
printf ( ” i = %d\n”, i ) ;
}
C
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 10 / 24
Pointers Pointer Arithmetic and Iterators Summary
#include <stdio.h>
int main()
{ Outputs:
0 1
0
int i =999;
int ∗ p i ;
i: 999
1
p i =&i;
printf ( ” i : %d\n”,i ) ; address of i: 0x7ffe3ef6a1cc
printf ( ”address of i : %p\n”, &i);
P
value of p i: 0x7ffe3ef6a1cc
printf ( ” value of p i : %p\n”, p i) ;
printf ( ” value of &p i: %p\n”, &p i); value of &p i: 0x7ffe3ef6a1d0
printf ( ” value of ∗ p i : %d\n”, ∗p i) ; value of *p i: 999
∗ p i = 1010;
M
printf ( ” i : %d\n”,i ) ;
C
printf ( ” value of ∗ p i : %d\n”, ∗p i) ;
printf ( ” &i: %p\n”, &i);
printf ( ” value of p i : %p\n”, p i) ;
return(0) ;
1010
1010
0x7ffe3ef6a1cc
0x7ffe3ef6a1cc
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 11 / 24
Pointers Pointer Arithmetic and Iterators Summary
0 1
and the variable name
10
The character * can appear anywhere between the data type name
P
int* p, q;
Above, only p is the pointer variable, not q.
q is an int variable.
Example: char c;
M
null pointer (NULL ) is special pointer value that does not refer to any
C
char* cp = NULL; // cp is pointer to char, initialized
to NULL
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 12 / 24
Pointers Pointer Arithmetic and Iterators Summary
0 1
int *x;
*x=3;
10
−→ Error! x doesn’t point to anything.
P
int foo;
int *x;
x = &foo;
*x=3;
C M −→ This is fine. x points to foo.
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 13 / 24
Pointers Pointer Arithmetic and Iterators Summary
Example
#include <stdio.h>
int main()
{
0 1
0
int var = 10;
int ∗ ptr = &var;
P
printf ( ”Address of var = %p\n”, ptr) ;
return 0;
C
}
Outputs:
Value of Var = 10
Address of Var = 0x7fffa057dd4
After doing *ptr = 20, *ptr is 20
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 14 / 24
Pointers Pointer Arithmetic and Iterators Summary
Pointer Arithmetic
0 1
0
pointer may be:
incremented ( ++ )
decremented ( −− )
P 1
an integer may be added to a pointer ( + or += )
an integer may be subtracted from a pointer ( += or -= )
C M
Pointer arithmetic is performed on an array.
Pointer arithmetic is meaningless unless performed on an array.
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 15 / 24
Pointers Pointer Arithmetic and Iterators Summary
Recall: Arrays
0 1 int foo[6];
10
E.g., array of int, double, char, . . .
C
We refer to individual elements, by
giving the index of the element in the
array
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 16 / 24
Pointers Pointer Arithmetic and Iterators Summary
Pointer Arithmetic
Integer math operations can be used with pointers (++, +=, –, -=)
0 1
it points to
10
If you increment a pointer, it will be increased by the size of whatever
P
int a[5] = {61, 14, 25, 36, 19};
int * ptr = a;
*ptr
a[0] a[1]
C M *(ptr+2)
a[2] a[3]
*(ptr+4)
a[4]
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 17 / 24
Pointers Pointer Arithmetic and Iterators Summary
Iterate over an integer array and display the address of every entry
0 1 Outputs:
int main()
{
int i [10], ∗ intPtr ;
10 0x7ffc8b014ab0
0x7ffc8b014ab4
intPtr = i ;
M
0x7ffc8b014ac0
{
printf ( ”%p \n”, intPtr + x ) ;
0x7ffc8b014ac4
0x7ffc8b014ac8
C
}
} 0x7ffc8b014acc
0x7ffc8b014ad0
0x7ffc8b014ad4
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 18 / 24
Pointers Pointer Arithmetic and Iterators Summary
0 1 Outputs:
0
int main()
{ 0x7ffc8b014ae0
1
double d[10], ∗doublePtr; 0x7ffc8b014ae8
doublePtr = d; // doublePtr points to the first 0x7ffc8b014af0
element of d
P
0x7ffc8b014af8
for ( int x=0; x < 10; x++ ) 0x7ffc8b014b00
{ 0x7ffc8b014b08
}
}
C M
printf ( ”%p \n”, doublePtr + x ) ; 0x7ffc8b014b10
0x7ffc8b014b18
0x7ffc8b014b20
0x7ffc8b014b28
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 19 / 24
Pointers Pointer Arithmetic and Iterators Summary
This time iterate over the integer to display the values of its entries
0 1
0
int main() Outputs:
{
1
int i [10] = {1,2,3,4,5,6,7,8,9,10}; 11
int ∗ intPtr = i ;
22
for ( int x=0; x < 10; x++ )
{
P 33
44
M
printf ( ”%d \n”, ∗( intPtr + x) ) ; 55
}
66
for ( int x=0; x < 10; x++ )
C
{ 77
printf ( ”%d \n”, i [x] ) ; 88
} 99
}
10 10
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 20 / 24
Pointers Pointer Arithmetic and Iterators Summary
Pointer Comparisons
Pointers may be compared using relational operators, such as: !=, ==
, <, and >.
int main()
0 1
0
{
int num[10], ∗ start , ∗end;
1
start = num;
end = &num[9];
while ( start != end )
{
P
printf ( ”Enter a number: ” ) ;
scanf ( ”%d”, start ) ;
}
start ++;
start = num;
C M
while ( start != end )
{
printf ( ”%d \n”, ∗ start ) ;
start ++;
}
}
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 21 / 24
Pointers Pointer Arithmetic and Iterators Summary
int main()
0 1
0
{
int num[10];
1
int ∗ start , ∗end;
start = num;
end = &num[9]; Note: Pointer arithmetic
while( (end − start) > 0)
{
printf ( ”Enter a number: ” ) ;
P only makes sense if
done on the same array!
}
}
C M
scanf ( ”%d”, start ) ;
start ++;
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 22 / 24
Pointers Pointer Arithmetic and Iterators Summary
10
All relational operators can be used for comparing pointers of same
P
type
All equality and inequality operators can be used with all pointer types
C M
Pointers cannot be divided or multiplied!
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 23 / 24
Pointers Pointer Arithmetic and Iterators Summary
About Pointers...
0 1
10
Pointers do not give information about pointed array sizes
The compiler has no way to know that an ‘int *’ points to an array of 10
P
values and not to a single integer.
C M
Ece Gelal Soyak · Bahcesehir University CMP1001 · Week 11 – Dec. 22, 2020 24 / 24