Exercise 3-18 (IndexArrays, overlapping arrays)

Chapter_3     Exercise_3-17     SimpleArrays StructArray     Exercise_3-19







Exercise 3-18     TCP1, p. 228-229


Exercise 3-18. Create a program that defines two int arrays, one right after the other. Index off the end of the first array into the second, and make an assignment. Print out the second array to see the changes caused by this. Now try defining a char variable between the first array definition and the second, and repeat the experiment. You may want to create an array printing function to simplify your coding.




CONTENTS:     indexarrays.c     IndexArrays.cpp




indexarrays.c         download


#include <stdio.h> // for printf(), putchar()
// print int array, knowing its name and size:
void printArray(int array[], char name[], int size);

int main()
{
int a[2] = {}, b[2] = {}; // initialize elements to 0
printArray(a, "a", 2); printArray(b, "b", 2);
a[2] = 1, a[3] = 2;
printArray(a, "a", 4); printArray(b, "b", 2);
b[-1] = -1, b[-2] = -2;
printArray(a, "a", 4); printArray(b, "b", 2);
printf("a = %p\n", a); printf("b = %p\n", b); // print addresses
putchar('\n');

int c[3] = {}; // initialize elements to 0
char x = 'x'; // &x < c (address of array c[])
int d[3] = {}; // initialize elements to 0
printArray(c, "c", 5); printArray(d, "d", 3);
c[3] = 1, c[4] = 2;
printArray(c, "c", 5); printArray(d, "d", 3);
d[-1] = -1, d[-2] = -2;
printArray(c, "c", 5); printArray(d, "d", 3);
printf("&x = %p\n", &x); // print addresses
printf(" c = %p\n", c); printf(" d = %p\n", d);

return 0;
}
// print int array, knowing its name and size:
void printArray(int array[], char name[], int size)
{
int i;

for (i = 0; i < size; i++)
{printf("%s[%d] = %d, ", name, i, array[i]);}
putchar('\n');
}
/*
gcc indexarrays.c -o indexarrays
./indexarrays
a[0] = 0, a[1] = 0,
b[0] = 0, b[1] = 0,
a[0] = 0, a[1] = 0, a[2] = 1, a[3] = 2,
b[0] = 1, b[1] = 2,
a[0] = -2, a[1] = -1, a[2] = 1, a[3] = 2,
b[0] = 1, b[1] = 2,
a = 0x7fffb80c36f0 // consecutive addresses: 8 bytes difference
b = 0x7fffb80c36f8 // (size of int = 4)

c[0] = 0, c[1] = 0, c[2] = 0, c[3] = 0, c[4] = 0,
d[0] = 0, d[1] = 0, d[2] = 0,
c[0] = 0, c[1] = 0, c[2] = 0, c[3] = 1, c[4] = 2,
d[0] = 1, d[1] = 2, d[2] = 0,
c[0] = 0, c[1] = -2, c[2] = -1, c[3] = 1, c[4] = 2,
d[0] = 1, d[1] = 2, d[2] = 0,
&x = 0x7fffb80c36ef
c = 0x7fffb80c3700 // consecutive array addresses: c = 12 in base 10
d = 0x7fffb80c370c // (size of int = 4)
*/





Note:  See ch3-ArrayArguments for the function printArray().











IndexArrays.cpp         download


#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
// print int array, knowing its name and size:
void printArray(int array[], string name, int size);

int main()
{
int a[2] = {}, b[2] = {}; // initialize elements to 0
printArray(a, "a", 2); printArray(b, "b", 2);
a[2] = 1, a[3] = 2;
printArray(a, "a", 4); printArray(b, "b", 2);
b[-1] = -1, b[-2] = -2;
printArray(a, "a", 4); printArray(b, "b", 2);
cout << "a = " << a << endl; cout << "b = " << b << endl; // print addresses
cout << endl;

int c[3] = {}; // initialize elements to 0
char x = 'x'; // &x < c (address of array c[])
int d[3] = {}; // initialize elements to 0
printArray(c, "c", 5); printArray(d, "d", 3);
c[3] = 1, c[4] = 2;
printArray(c, "c", 5); printArray(d, "d", 3);
d[-1] = -1, d[-2] = -2;
printArray(c, "c", 5); printArray(d, "d", 3);
cout << "&x = " << static_cast<void *>(&x) << endl; // print addresses
cout << " c = " << c << endl; cout << " d = " << d << endl;

return 0;
}
// print int array, knowing its name and size:
void printArray(int array[], string name, int size)
{
for (int i = 0; i < size; i++)
{cout << name << "[" << i << "] = " << array[i] << ", ";}
cout << endl;
}
/*
g++ IndexArrays.cpp -o IndexArrays
./IndexArrays
a[0] = 0, a[1] = 0,
b[0] = 0, b[1] = 0,
a[0] = 0, a[1] = 0, a[2] = 1, a[3] = 2,
b[0] = 1, b[1] = 2,
a[0] = -2, a[1] = -1, a[2] = 1, a[3] = 2,
b[0] = 1, b[1] = 2,
a = 0x7ffd72ca3918 // consecutive addresses: (2*16+0)-(1*16+8) = 8 bytes
b = 0x7ffd72ca3920 // (size of int = 4)

c[0] = 0, c[1] = 0, c[2] = 0, c[3] = 0, c[4] = 0,
d[0] = 0, d[1] = 0, d[2] = 0,
c[0] = 0, c[1] = 0, c[2] = 0, c[3] = 1, c[4] = 2,
d[0] = 1, d[1] = 2, d[2] = 0,
c[0] = 0, c[1] = -2, c[2] = -1, c[3] = 1, c[4] = 2,
d[0] = 1, d[1] = 2, d[2] = 0,
&x = 0x7ffd72ca3916
c = 0x7ffd72ca3928 // consecutive array addresses: (3*16+4)-(2*16+8)=12 bytes
d = 0x7ffd72ca3934 // (size of int = 4)
*/





Note:  cout << &x does not print the address of char x because &x is interpreted as a char* or string. See display_char_address on StackOverflow.









Chapter_3     Exercise_3-17     SimpleArrays BACK_TO_TOP StructArray     Exercise_3-19



Comments

Popular posts from this blog

Contents