1 / 21

Pointers

Pointers . EE2372 Dr. Gerardo Rosiles. 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.

mickey
Télécharger la présentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Each one has its own address (or number) so that you know were a specific message (or value) needs to be placed. Address 0 1 2 3 3,999,999 4,000,000 Memory cell  

  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 0 Han 2 Luke 3PO 4,000,000 Memory cell 20 “Hello” 3.1416  

  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; 5 2 1 3 6 Address 0 0 21 0 Memory cell age 4 0 The address of a variable is the address of the first byte used to store the variable value.

  7. Reviewing the memory model of computers • This “alias” to the address is what we call in programming an identifier or variable name. 112 110 Address 111 115 116 113 118 119 120 121 123 124 117 ‘e’ ‘\0’ ‘l’ ‘l’ ‘H’ Memory cell ‘A' ch word[ ] pi 3.14 is represented with a special binary format. 114 122 ‘o’

  8. & = The “address of” operator C allows to access the address of variables using 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

  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; value 30 variable name 0 age age_ptr Memory cell 30 1  

  15. Pointers • Example word_ptr = &(word[0]) ‘l’ ‘l’ variable name word_ptr word [ ] Memory cell 110 ‘H’ ‘e’   ‘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_ptraccess the value of 10 from variable age

  18. 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); }

  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: structstudent_data { char name[SIZE]; int age; float grade; };

More Related