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 DocumentRight Arrow Icon
EECE 230 – Notes by: S. Karaki 2 In the declaration, the * associates with the variable so int *countPtr, count declares an integer pointer countPtr and an integer variable count. To declare two float pointers vP1 and vP2, we have to write it as follows: float *vP1, *vP2; So the operator * does not distribute to all variables in a declaration. A pointer may be initialized to 0, NULL or an address. When 0 is assigned to a pointer it is converted to NULL, i.e. pointing nowhere. 5.3 Pointer Operators The address operator & returns the address of its operand. For the example in the above program, the statement assigns the address of the variable count to the pointer variable countPtr . The address operator & cannot be applied to constants or to expressions that do not result in references. The * operator, known as the dereference or indirection operator, returns the value of the object pointed at. So the program above, the statement cout << *countPtr << endl; prints the value of the variable count . This is called dereferencing the pointer countPtr. The & and * operators are complements of one another. The following simple program can prove this. Example 5.2: // The & and * are complementary operators. #include <iostream> using std::cout; using std::endl; void main( ) { int *vP, v= 7; vP= &v; cout << v << endl; // print the variable v cout << vP << endl; // print the value of vP cout << *vP << endl; // print the value of v using *vP cout << &*vP << endl; // print the value of vP cout << *&vP << endl; // print the value of vP } The output of the above program is: 7 0xfff4 7 0xfff4 0xfff4 5.4 Calling Functions by Reference
Background image of page 2
EECE 230 – Notes by: S. Karaki 3 There are three ways to pass arguments to a function: - Call by value;
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online