COM SCI 31 Lecture Notes - Lecture 14: Memory Address, Undefined Behavior

39 views2 pages
21 Nov 2017
School

Document Summary

For every variable declared, the compiler will put it somewhere in memory. *&x; // this returs the object pointed to by the pointer that. &*p; // this returns the pointer to the object that p is pointing. //to (this only works if p is a pointer), or just p. When iterating using pointers, the following logic applies: int da [10]; for (dp = &da[0]; dp < &da[maxsize]; dp++){ /* (the pointer dp is less than the element of the last element in the array) **** */ The code above will set each element of da to 3. 6, incrementing the pointer with each iteration. How this works: dp = &da[i]; dp++ = dp + 1; dp++ = &da[i] + 1; /* (what this actually does is add 8 bytes to the address referenced by dp. ) The same logic applies when using the -- operator as well. dp = &da[i]; dp-- = dp - 1; dp-- = &da[i] - 1;

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents