Docsity

Pointer Constants and Pointer Variables-Introduction to Programming-Lab Mannual, Exercises of Computer Programming

Partial preview of the text

Download Pointer Constants and Pointer Variables-Introduction to Programming-Lab Mannual and more Exercises Computer Programming in PDF only on Docsity!

#include

using namespace std;

int main()

int intarray[5] = { 31, 54, 77, 52, 93 };

for(int j=0; j<5; j++)

cout << *(intarray+j) << endl;

return 0;

Pointer Constants and Pointer Variables 

intarray is a pointer constant. You can’t say

intarray++ any more than you can say 7++.

But while you can’t increment an address, you

can increment a pointer that holds an address.

Pointer Constants and Pointer Variables #include using namespace std; int main() { int intarray[] = { 31, 54, 77, 52, 93 }; //array int* ptrint; ptrint = intarray; for(int j=0; j<5; j++) cout << *(ptrint++) << endl; return 0; }

#include

using namespace std;

void centimize(double*);

int main()

double var = 10.0;

cout << “var = ” << var << “ inches” << endl;

centimize(&var);

cout << “var = ” << var << “ centimeters” << endl;

return 0;

void centimize(double* ptr)

*ptr *= 2.54;

} docsity.com

#include

using namespace std;

void centimize(double*);

const int MAX = 5;

int main()

double varray[MAX] =

centimize(varray);

for(int j=0; j<MAX; j++)

cout << “varray[” << j << “]=” << varray[j] << “ centimeters” << endl;

return 0;

void centimize(double* ptrd) { for(int j=0; j<MAX; j++) *ptrd++ *= 2.54; } docsity.com

LAB TASK

Suppose you have a main() with three local arrays,

all the same size and type (say float). The first two

are already initialized to values. Write a function

called addarrays() that accepts the addresses of

the three arrays as arguments; adds the contents

of the first two arrays together, element by

element; and places the results in the third array

before returning. A fourth argument to this

function can carry the size of the arrays. Use

pointer notation throughout; the only place you

need brackets is in defining the arrays.