Pointers and Structures in Programming Fundamentals

ece 244 n.w
1 / 10
Embed
Share

Explore the concepts of pointers and structures in programming fundamentals with examples and clarifications. Learn about memory addresses, accessors, mutators, and the use of pointers to pointers. Gain insights into memory management and pointer manipulation in C++ programming.

  • Pointers
  • Structures
  • Programming
  • Memory Management
  • C++

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ECE 244 Programming Fundamentals Lec. 10: Pointers and Structures Ding Yuan ECE Dept., University of Toronto http://www.eecg.toronto.edu/~yuan

  2. Clarifications: Accessors & Mutators Accessors and Mutators Member functions that get or set private member variables class Time { private: int minute; int second; public: void resetTime(); void setSeconds(int); void setMinutes(int); int getSeconds(); int getMinutes(); void printTime(); }; Mutators Accessors

  3. delete [] When allocate using new [], must free using delete [] When allocate using new (without []), must free with delete without [] Otherwise undefined behavior class B { private: char *name; public: B(); B(char*); ~B(); }; B::B (char *_name) { name = new char[100]; strcpy(name, _name); } B::~B() { if (name != NULL) delete [] name; }

  4. What is a pointer? A memory address (to a byte of memory) Memory: addr. : 0 1 2 3 4 5 6 7 8 9 You can change the content, but the address remains the same Pointer in real-world Cell #: 416-978-5033 Upgrade your phone

  5. s p int s; int *p; Memory: garbage garbage addr. : 0F 10 11 12 13 14 15 16 17 18 Question: what is the value of &s? &p? s p s = 0; p = NULL; Memory: 0 0 addr. : 0F 10 11 12 13 14 15 16 17 18 Question: what is the value of &s? &p? s p p = &s; Memory: 0 0x10 addr. : 0F 10 11 12 13 14 15 16 17 18 Question: what is the value of *p? p? &p? s p *p = 100; Memory: 100 0x10 p is dereferenced addr. : 0F 10 11 12 13 14 15 16 17 18

  6. Pointers to pointers (to pointers to ) The same as address of the a pointer q is a pointer to a pointer. Like any other pointer, this just contains an address It tells the compiler that it contains the address of a pointer so that the compiler can detect when we make mistake in the code int c = 0; int s = 0; int *p = NULL; int **q = NULL; q p c s 0 0 0 0 Memory: addr. : 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22

  7. Use q to set s to 100 q = &p; p = &s; **q = 100; Memory content before executing these three lines of code q p c s 0 0 0 0 Memory: addr. : 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22

  8. Use q to set s to 100 q = &p; p = &s; **q = 100; int c = 0, s = 0; int *p = NULL; int **q = NULL; char c = **q; // type mismatch Memory content before executing these three lines of code q p c s 0 0 0 0 Memory: addr. : 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 Memory content after executing these three lines of code q p c s 0 Memory: 0x14 100 0x18 addr. : 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22

  9. Pointer & Structures struct Node { int data; struct Node *next; /* a pointer to struct Node */ }; Pointers to structures are declared in the same way as any other pointer struct Node node; struct Node *p; p = &node; node p data next garbage garbage Memory: 0x10 addr. : 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22

  10. Pointer & Structures (cont.) struct Node { int data; struct Node *next; /* a pointer to struct Node */ }; Instead of using a . , use -> to find the destination of a structure pointer p->data = 100; p->next = NULL; node p data next Memory: 0x10 100 0 addr. : 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22

More Related Content