Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Pointers EE2372 Dr. Gerardo Rosiles. Introduction Pointers are one of the most advanced features in the C-language. Allows to generalize code so different."— Presentation transcript:

1 Pointers EE2372 Dr. Gerardo Rosiles

2 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.

3 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.

4 Reviewing the memory model of computers Memory can be viewed as a set of stacked mailboxes or cells. Address Memory cell     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.

5 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 0 Han Luke “Hello”     3PO ,000,000

6 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 The address of a variable is the address of the first byte used to store the variable value. age

7 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’ ‘A' word[ ] ch pi 3.14 is represented with a special binary format.

8 & = 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

9 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?

10 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

11 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.

12 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.

13 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.

14 Pointers Example int 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 0 age 30 age_ptr 1 1     value 30

15 Pointers Example word_ptr = &(word[0]) variable name Memory cell word_ptr 110 word [ ] ‘H’ ‘e’     ‘l’ ‘o’

16 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.

17 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 2 and *age_ptr access the value of 10 from variable age

18 Example //pointers 1 #include 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); }

19 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)

20 Example See pointers2.c

21 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; };


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

Similar presentations


Ads by Google