Pointers Linyux
Pointers Linyux
Pointers
Pointers
● Pointers are one of the most sophisticated features of the C
programming language.
2
A
Defining a Pointer Variable
10
Suppose you define a variable as follows:
int
int count = 10; count
3
D A
Defining a Pointer Variable
A 10
Suppose you define a variable as follows:
int * int
int_point
int count = 10; er
count
7
D A
Retrieving a Pointer Value
A 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
?
int
value
8
D A
Retrieving a Pointer Value
A 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
?
This dereferences the
int
pointer. In other words, it
value
“follows” the pointer to
return the value.
9
D A
Retrieving a Pointer Value
A 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
?
Thus, the value that results int
from evaluating this value
expression is 10.
10
D A
Retrieving a Pointer Value
A 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
10
10 is then assigned to the int
left-hand side of the value
assignment.
11
D A
Retrieving a Pointer Value
A 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
int_pointer = &value; 10
int
What do you think this
value
does?
12
D A
Retrieving a Pointer Value
A 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
int_pointer = &value; 10
First, we get the address of int
the integer variable value, value
which in this case is B.
13
D A
Retrieving a Pointer Value
B 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
int_pointer = &value; 10
Then, we assign the
int
address B to the memory
value
location represented by the
variable int_pointer.
14
D A
Retrieving a Pointer Value
B 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
int_pointer = &value; 10
*int_pointer = 86; Now, what do you think this int
value
does?
15
D A
Retrieving a Pointer Value
B 10
The prefix * operator retrieves a pointer value.
int * int
int_point
int count = 10; er
count
17
More Pointer Basics
18
More Pointer Basics
Q Q
19
More Pointer Basics
Q Q
20
More Pointer Basics
Q Q
/ /
21
More Pointer Basics
Q Q
/ /
22
Play with the code!
23
Working with Pointers and Structures
You have seen how a pointer can be
defined to point to a basic data type, such
as an int or a char.
struct date {
int month;
int day;
int year;
}
24
Working with Pointers and Structures
You have seen how a pointer can be struct date today = {9, 25, 2004};
defined to point to a basic data type, such
as an int or a char.
struct date {
int month;
int day;
int year;
}
25
Working with Pointers and Structures
You have seen how a pointer can be struct date today = {9, 25, 2004};
defined to point to a basic data type, such
as an int or a char. struct date *datePtr;
struct date {
int month;
int day;
int year;
}
26
Working with Pointers and Structures
You have seen how a pointer can be struct date today = {9, 25, 2004};
defined to point to a basic data type, such
as an int or a char. struct date *datePtr;
struct date {
int month;
int day;
int year;
}
27
Working with Pointers and Structures
You have seen how a pointer can be struct date today = {9, 25, 2004};
defined to point to a basic data type, such
as an int or a char. struct date *datePtr;
struct date {
int month;
int day;
int year;
}
21
28
Working with Pointers and Structures
You have seen how a pointer can be struct date today = {9, 25, 2004};
defined to point to a basic data type, such
as an int or a char. struct date *datePtr;
29