pointers ee2372 dr. gerardo rosiles. introduction pointers are one of the most advanced features in...

21
Pointers EE2372 Dr. Gerardo Rosiles

Upload: hugh-bond

Post on 16-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers

EE2372Dr. Gerardo Rosiles

Page 2: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Introduction

• Pointers are one of the most advanced features in the C-language.

• Allows to generalize code so different types of parameters can be passed to the same function.

• Allows to efficiently represent and link structures.• Allows to deal with variables created on the fly.

(Dynamically allocated variables).• Makes code very fast.

Page 3: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Indirection• To understand pointers we must understand the concept of

indirection.

• indirection as on indirect …. like getting something done through a third party.

• In soccer: Indirect free shot. Any other ideas?

• Wikipedia says: indirection is the ability to reference something using a name, reference, or container instead of the value itself….…..The most common form of indirection is the act of manipulating a

value through its memory address.

Page 4: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Reviewing the memory model of computers

• Memory can be viewed as a set of stacked mailboxes or cells.

Address Memory cell

0

1

2

3

3,999,999

4,000,000

Each one has its own address (or number) so that you know were a specific message (or value) needs to be placed.

Page 5: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Reviewing the memory model of computers

• Instead of an address which may be hard to remember, occupied mailboxes have the name of the owner.

Address Memory cell

0

Han 20

2

Luke “Hello”

3PO 3.1416

4,000,000

Page 6: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Reviewing the memory model of computers

• This “alias” to the address is what we call in programming an identifier or variable name.

int age=25;char word[] = “Hello”;char ch = ‘A’;float pi = 3.14;

Address Memory cell

0

1

2 21

3 0

4 0

5 0

6

The address of a variable is the address of the first byte used to store the variable value.

age

Page 7: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Reviewing the memory model of computers

• This “alias” to the address is what we call in programming an identifier or variable name.

Address Memory cell

110 ‘H’

111 ‘e’

112 ‘l’

113 ‘l’

114 ‘o’

115 ‘\0’

116

117

118 ‘A'

119

120

121

122

123

124

word[ ] ch

pi3.14 is represented with a special binary format.

Page 8: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

& = The “address of” operator

• &age is 2

• &word is 110

• &(word[0]) is 110• &(word[1]) is 111 • &(word[2]) is 112 • &(word[3]) is 113 • &(word[4]) is 114

• &ch is 118

• &pi is 120

C allows to access the address of variables using the “address of” operator

Page 9: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

What do we do with addresses in C?

• Addresses ten to be very large numbers. That is true now more than ever.

• If you have 2 GB of memory, how large are the largest addresses?

• If C allows us to get the address of a variable, …

• how do we store it?

Page 10: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Slide 10: In which we arrive to POINTERS

• There is a special type of variable in C called pointers.• Pointers are used to store addresses.• We use the indirection operator * to create them.• There are pointers for each C type:

int *age_ptr; //a pointer of type int

char *word_ptr; //a pointer to a character // which is”linked” to an string

char *ch_ptr; // a pointer to a character

float *myval_ptr;// a pointer to a float number

Page 11: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers

• All memory addresses are equal.

• However, what we put in memory is varied and we need to specify what type of object we are storing on a particular address.

Page 12: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers

• Why do we call them pointers?

A pointer is a variable whose content is an address that points to another location in memory which contains an actual value of the specified pointer type.

Page 13: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers• So what is the all this indirection deal about?

• Pointers can store address of variables AND• most importantly, we can access the value of variables

through pointers!

• How? Use the indirection operator.

age_ptr = &age; // load the address of age to // pointer*age_ptr = 10; // the value of age NOT age_ptr

// is changed.

Page 14: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers

• Exampleint age=20, *age_ptr; age_ptr = &age;*age_ptr = 10;age = 30; value = age;value = age_ptr;value = *age_ptr;

variable name Memory cell

0

age 30

age_ptr 1

value 30

Page 15: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers

• Example word_ptr = &(word[0]) variable name Memory cell

word_ptr 110

word [ ] ‘H’

‘e’

‘l’

‘l’

‘o’

Page 16: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers• Question: What is the content of a pointer when it is created?• It is undetermined.

• It is pointing to nowhere in memory, or to some dangerous place which can bring down your system.

• A pointer should not be used until it is properly loaded with a meaningful address.

• Pointers can be fatal if not used properly.

Page 17: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Pointers

• There is a confusing way to initialize pointers during the pointer declaration:

int age = 10;int *age_ptr = &age;

• We are creating a pointer of type int AND initializing it with the address of age so

age_ptr contains address 2and

*age_ptr access the value of 10 from variable age

Page 18: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Example//pointers 1#include <stdio.h>#include<conio.h>

int main(void){ int count = 10, x; int *int_pointer; int_pointer = &count; x = *int_pointer; printf("count = %i, x = %i \n", count, x); printf("address of count = %lu \n", int_pointer); printf("address of count in hex = %lx \n", int_pointer);}

Page 19: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Re-using pointer

• Pointers are container for address, so they can be reused through your program to store different addresses

• (As long as they are of the same C type)

Page 20: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Example

• See pointers2.c

Page 21: Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different

Working with pointers and structures

• We will see that combining pointers with structures allows very powerful C constructs.

• Recall this structure:

struct student_data { char name[SIZE]; int age; float grade; };