0% found this document useful (0 votes)
9 views29 pages

Pointers Linyux

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

Pointers Linyux

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

Programming in C

Pointers
Pointers
● Pointers are one of the most sophisticated features of the C
programming language.

● The power and flexibility that C provides in dealing with pointers


serve to set it apart from many other programming languages.

● Pointers enable you to:


○ Effectively represent complex data structures
○ To change values passed as arguments to functions
○ To work with memory that has been allocated “dynamically”
○ Concisely and efficiently deal with arrays.

2
A
Defining a Pointer Variable
10
Suppose you define a variable as follows:
int
int count = 10; count

This reserves space in memory at some address A that is large enough


to hold an integer value, in this case, the value 10.

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

This reserves space in memory at some address A that is large enough


to hold an integer value, in this case, the value 10.

We can then define a pointer as follows:

int* int_pointer = &count;

This reserves space in memory that is large enough to hold a pointer


value. A pointer value is an address of a location in memory.
4
D A
Defining a Pointer Variable
A 10
Suppose you define a variable as follows:
int * int
int_point
int count = 10; er
count

This reserves space in memory at some address A that is large enough


to hold an integer value, in this case, the value 10.

We can then define a pointer as follows:

int* int_pointer = &count;

The & when prepended to a variable is the address of operator. It


returns the address of the variable it operates on.
5
D A
Defining a Pointer Variable
A 10
Suppose you define a variable as follows:
int * int
int_point
int count = 10; er
count

This reserves space in memory at some address A that is large enough


to hold an integer value, in this case, theThe
value 10. count is an
variable
We can then define a pointer as follows:int, so &count is a
pointer to an int, that is,
int* int_pointer = &count; an int *.

The & when prepended to a variable is the address of operator. It


returns the address of the variable it operates on.
6
D A
Defining a Pointer Variable
A 10
Here is another depiction of what this looks like.
int * int
int_point
int count = 10; er
count

int* int_pointer = &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* int_pointer = &count;


B
int value = *int_pointer;

?
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

int *int_pointer = &count;


B
int value = *int_pointer;

?
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

int *int_pointer = &count;


B
int value = *int_pointer;

?
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

int *int_pointer = &count;


B
int value = *int_pointer;

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 *int_pointer = &count;


B
int value = *int_pointer;

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 *int_pointer = &count;


B
int value = *int_pointer;

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 *int_pointer = &count;


B
int value = *int_pointer;

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 *int_pointer = &count;


B
int value = *int_pointer;

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

int *int_pointer = &count;


B
int value = *int_pointer;

int_pointer = &value; A dereference of a pointer 86


on the left-side of an
*int_pointer = 86; assignment evaluates to int
value
the location where we can
store a value into. Here, B.
16
More Pointer Basics

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!

More Pointer Basics


Q Q
/ /
( (

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.

But pointers can also be defined to point


to structures.

Remember this struct:

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.

But pointers can also be defined to point


to structures.

Remember this struct:

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;

But pointers can also be defined to point


to structures.

Remember this struct:

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;

But pointers can also be defined to point datePtr = &today;


to structures.

Remember this struct:

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;

But pointers can also be defined to point datePtr = &today;


to structures.
(*datePtr).day = 21;
Remember this struct:

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;

But pointers can also be defined to point datePtr = &today;


to structures.
(*datePtr).day = 21;
Remember this struct:
datePtr->day = 21; // the more
struct date { usual form
int month;
int day;
int year;
}
21

29

You might also like