Programming and Graphics

(Kiana) #1

136 Introduction to C++ Programming and Graphics


cout << memad1 <<""<<memad2 << " "<< memad3 <<endl;
cout << *memad1 <<""<<*memad2 << " "<< *memad3 <<endl;
return 0;
}

The output of the code is:


0xbf9c6e48 0xbf9c6e50 0xbf9c6e58
1.1 1.2 1.3

Strings


Pointers of string variables behave in a similar fashion. Consider the
following code contained in the filepointerstring.cc:


#include <iostream>
using namespace std;

int main()
{
string onoma = "iakovos";
string * memad = &onoma;
cout << onoma << endl;
cout << memad << endl;
return 0;
}

Running the executable produces the output:


iakovos
0xbfa845f4

In this example, 0xbfa845f4 is the memory address of the first letter of the
stringiacovos.


String to character conversion


We can use pointers to convert a string variable to a character array.
First, we find the length of the string using thelengthfunction and introduce
an equal-sized character array. Second, we run through successive pointers of
the string characters while evaluating successive elements of the character array,
as illustrated in the following block:


string onoma= "arkouditsa";
int l = onoma.length();
Free download pdf