Programming and Graphics

(Kiana) #1

134 Introduction to C++ Programming and Graphics


It would seem that this method can be used to extract the content of a
given memory address by stating, for example,


cout << b <<""<<mab<<""<<*(char*) 234 << endl;

However, since we do not know the data type stored in that address, we will be
greeted with the dreaded segmentation fault.


Problems


5.1.1.Assess the data types of the variables p1 and p2 declared in the line:


int * p1, p2;

5.1.2.Initialize a pointer to a data type of your choice as NULL, and then
print and discuss its value.


5.1.3.A collection of pointers to the same data type can be accommodated in
a vector array. Write a program that evaluates and prints such an array.


5.1.4.Write a program that defines and prints the fourth-order pointer of a
character.


5.2 Pointerstoarraysandstrings....................


To locate a vectorvin memory, we require the address of the first element,
v[0]. Subsequent elements are located in consecutive memory addresses.


The following code contained in the filepointervector.ccextracts the
address of a vector and confirms that it is equal to the address of the first
element:


#include <iostream>
using namespace std;

int main()
{
double A[4]={1.1, 1.2, 1.3, 1.4};
double * memad1 = A;
double * memad2 = &A[0];
cout << memad1 <<""<<memad2 << endl;
return 0;
}

The output of the code is:


0xbf83b6c8 0xbf83b6c8
Free download pdf