{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

strings and pointers - EECE 230 S Karaki 1 5 Pointers and...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
EECE 230 – S. Karaki 1 5. Pointers and Strings 5.1 Introduction Pointers enable programs to simulate call by reference, and to create and manipulate dynamic data structures, which can grow and shrink in size, e.g. linked lists, queues, stacks, and trees. This chapter explains basic pointer concepts and investigates the relationship among arrays, pointers and strings. 5.2 Declaration and Initialization of Pointers A pointer is a variable that has as content (r-value) the address (l-value) of some other variable or object. For example in Fig. 5.1 below the variable count directly refers to a value and the pointer countPtr indirectly references a value. Pointers like other variables must be declared before being used as illustrated in the program below. 6 . countPtr count Fig. 5.1: A pointer to an integer variable. Example 5.1: //Declare and initialize pointers: #include <iostream> using std::cout; using std::endl; void main( ) { // Declare an integer variable and a pointer . int count= 6, *countPtr; // Make countPtr point to variable count countPtr= &count; // print out variables cout << count << endl; // print content using the variable cout << *countPtr << endl; // print content using the pointer } Output: 6 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon