CS 225 Lecture Notes - Lecture 5: Null Pointer, Dereference Operator, C Dynamic Memory Allocation

28 views5 pages

Document Summary

Pointer variables and dynamic memory allocation int x; int *p; p = &x; // int pointer type p now has the address of x p = new int; // similar to malloc in c. // p is a pointer that points to a block of memory size int. // we can only do p = new int, we cannot do p = new double. // once p points to a new block of memory, it won"t affect x. // we can assign pointer p a value with the dereference operator * // the benefit of using heap memory instead of stack memory is that the variable will be useable even after the program has left the scope of the function int *p, q; int *p, *q; // this is the proper syntax to make a int pointer q. Review int *p; int x; p = &x; *p = 6; cout << x << endl; cout << p << endl;

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents