#include <stdio.h> // for printf(), putchar()
int main()
{
int i;
int a[5] = {}; // initialize elements to 0
for (i = 0; i < 5; i++)
{printf("a[%d] = %d, ", i, a[i]);}
putchar('\n');
int b[5] = {0}; // initialize elements to 0
for (i = 0; i < 5; i++)
{printf("b[%d] = %d, ", i, b[i]);}
putchar('\n');
int c[5] = {0,5}; // initialize second element to 5, the rest to 0
for (i = 0; i < 5; i++)
{printf("c[%d] = %d, ", i, c[i]);} // 0,5,0,0,0
putchar('\n');
int d[5] = {0,1,2,3,4,5}; // warning: 6 initializers for 5 elements
for (i = 0; i < 5; i++)
{printf("d[%d] = %d, ", i, d[i]);} // 0,1,2,3,4
putchar('\n');
int e[5]; // uninitialized elements
for (i = 0; i < 5; i++)
{printf("e[%d] = %d, ", i, e[i]);} // garbage values
putchar('\n');
for (i = 0; i < 5; i++)
{
e[i] = i * 10; // initialize elements
printf("e[%d] = %d, ", i, e[i]); // 0,10,20,30,40
}
putchar('\n');
return 0;
}
/*
gcc simplearrays.c -o simplearrays
./simplearrays
a[0] = 0, a[1] = 0, a[2] = 0, a[3] = 0, a[4] = 0,
b[0] = 0, b[1] = 0, b[2] = 0, b[3] = 0, b[4] = 0,
c[0] = 0, c[1] = 5, c[2] = 0, c[3] = 0, c[4] = 0,
d[0] = 0, d[1] = 1, d[2] = 2, d[3] = 3, d[4] = 4,
e[0] = 0, e[1] = 0, e[2] = -1905635168, e[3] = 22013, e[4] = 774351232,
e[0] = 0, e[1] = 10, e[2] = 20, e[3] = 30, e[4] = 40,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a[5] = {}; // initialize elements to 0
for (int i = 0; i < 5; i++)
{cout << "a[" << i << "] = " << a[i] << ", ";}
cout << endl;
int b[5] = {0}; // initialize elements to 0
for (int i = 0; i < 5; i++)
{cout << "b[" << i << "] = " << b[i] << ", ";}
cout << endl;
int c[5] = {0,5}; // initialize second element to 5, the rest to 0
for (int i = 0; i < 5; i++)
{cout << "c[" << i << "] = " << c[i] << ", ";} // 0,5,0,0,0
cout << endl;
// int d[5] = {0,1,2,3,4,5}; // error: 6 initializers for 5 elements
int d[5] = {0,1,2,3,4};
for (int i = 0; i < 5; i++)
{cout << "d[" << i << "] = " << d[i] << ", ";} // 0,1,2,3,4
cout << endl;
int e[5]; // uninitialized elements
for (int i = 0; i < 5; i++)
{cout << "e[" << i << "] = " << e[i] << ", ";} // garbage values
cout << endl;
for (int i = 0; i < 5; i++)
{
e[i] = i * 10; // initialize elements
cout << "e[" << i << "] = " << e[i] << ", "; // 0,10,20,30,40
}
cout << endl;
return 0;
}
/*
g++ SimpleArrays.cpp -o SimpleArrays
./SimpleArrays
a[0] = 0, a[1] = 0, a[2] = 0, a[3] = 0, a[4] = 0,
b[0] = 0, b[1] = 0, b[2] = 0, b[3] = 0, b[4] = 0,
c[0] = 0, c[1] = 5, c[2] = 0, c[3] = 0, c[4] = 0,
d[0] = 0, d[1] = 1, d[2] = 2, d[3] = 3, d[4] = 4,
e[0] = 0, e[1] = 0, e[2] = 1863696640, e[3] = 21989, e[4] = -83429328,
e[0] = 0, e[1] = 10, e[2] = 20, e[3] = 30, e[4] = 40,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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.
#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)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// An array of struct
#include <iostream>
using std::cout;
using std::endl;
typedef struct
{
int i, j, k;
} ThreeDpoint;
void print3d(ThreeDpoint);
int main()
{
ThreeDpoint p[5];
for(int i = 0; i < 5; i++) // local i
{
print3d(p[i]); // local i
p[i].i = i + 1; // local i, struct i, local i
p[i].j = i + 2; // local i, struct j, local i
p[i].k = i + 3; // local i, struct k, local i
print3d(p[i]); // local i
cout << endl;
}
return 0;
}
void print3d(ThreeDpoint p)
{ // struct i, j, k:
cout << "(" << p.i << ", " << p.j << ", " << p.k << "), ";
}
/*
g++ StructArray.cpp -o StructArray
./StructArray
(-8325208, 32767, 1), (1, 2, 3), // garbage values, (1,2,3)
(0, -8325536, 32767), (2, 3, 4),
(-1114315774, 21904, 640), (3, 4, 5),
(0, 65535, 1), (4, 5, 6),
(-8325520, 32767, -1114315748), (5, 6, 7),
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf()
int main()
{
int i, a[10];
printf("sizeof(int) = %lu\n", sizeof(int));
for(i = 0; i < 10; i++)
{
printf("&a[%d] = %p (hex) = %ld (dec)\n", i, &a[i], (long)&a[i]);
}
return 0;
}
/*
gcc arrayaddresses.c -o arrayaddresses
./arrayaddresses
sizeof(int) = 4
&a[0] = 0x7ffc8a3b1ca0 (hex) = 140722627615904 (dec)
&a[1] = 0x7ffc8a3b1ca4 (hex) = 140722627615908 (dec)
&a[2] = 0x7ffc8a3b1ca8 (hex) = 140722627615912 (dec)
&a[3] = 0x7ffc8a3b1cac (hex) = 140722627615916 (dec)
&a[4] = 0x7ffc8a3b1cb0 (hex) = 140722627615920 (dec)
&a[5] = 0x7ffc8a3b1cb4 (hex) = 140722627615924 (dec)
&a[6] = 0x7ffc8a3b1cb8 (hex) = 140722627615928 (dec)
&a[7] = 0x7ffc8a3b1cbc (hex) = 140722627615932 (dec)
&a[8] = 0x7ffc8a3b1cc0 (hex) = 140722627615936 (dec)
&a[9] = 0x7ffc8a3b1cc4 (hex) = 140722627615940 (dec)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a[10];
cout << "sizeof(int) = "<< sizeof(int) << endl;
for(int i = 0; i < 10; i++)
{
cout << "&a[" << i << "] = " << &a[i] << " (hex) = "
<< (long)&a[i] << " (dec)" << endl;
}
return 0;
}
/*
g++ ArrayAddresses.cpp -o ArrayAddresses
./ArrayAddresses
sizeof(int) = 4
&a[0] = 0x7ffdfbd47720 (hex) = 140728828458784 (dec)
&a[1] = 0x7ffdfbd47724 (hex) = 140728828458788 (dec)
&a[2] = 0x7ffdfbd47728 (hex) = 140728828458792 (dec)
&a[3] = 0x7ffdfbd4772c (hex) = 140728828458796 (dec)
&a[4] = 0x7ffdfbd47730 (hex) = 140728828458800 (dec)
&a[5] = 0x7ffdfbd47734 (hex) = 140728828458804 (dec)
&a[6] = 0x7ffdfbd47738 (hex) = 140728828458808 (dec)
&a[7] = 0x7ffdfbd4773c (hex) = 140728828458812 (dec)
&a[8] = 0x7ffdfbd47740 (hex) = 140728828458816 (dec)
&a[9] = 0x7ffdfbd47744 (hex) = 140728828458820 (dec)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-19. Modify ArrayAddresses.cpp (see ch3-ArrayAddresses) to work with the data types char, long int, float, and double.
#include <stdio.h> // for printf()
int main()
{
char ca[2]; short sa[2]; // arrays
int i, ia[2]; long la[2]; long long lla[2];
float fa[2]; double da[2]; long double lda[2];
printf("sizeof(char) = %lu\n", sizeof(char));
for(i = 0; i < 2; i++)
{
printf("&ca[%d] = %p (hex) = %ld (dec)\n", i, &ca[i], (long)&ca[i]);
}
printf("sizeof(short) = %lu\n", sizeof(short));
for(i = 0; i < 2; i++)
{
printf("&sa[%d] = %p (hex) = %ld (dec)\n", i, &sa[i], (long)&sa[i]);
}
printf("sizeof(int) = %lu\n", sizeof(int));
for(i = 0; i < 2; i++)
{
printf("&ia[%d] = %p (hex) = %ld (dec)\n", i, &ia[i], (long)&ia[i]);
}
printf("sizeof(long) = %lu\n", sizeof(long));
for(i = 0; i < 2; i++)
{
printf("&la[%d] = %p (hex) = %ld (dec)\n", i, &la[i], (long)&la[i]);
}
printf("sizeof(long long) = %lu\n", sizeof(long long));
for(i = 0; i < 2; i++)
{
printf("&lla[%d] = %p (hex) = %ld (dec)\n", i, &lla[i], (long)&lla[i]);
}
printf("sizeof(float) = %lu\n", sizeof(float));
for(i = 0; i < 2; i++)
{
printf("&fa[%d] = %p (hex) = %ld (dec)\n", i, &fa[i], (long)&fa[i]);
}
printf("sizeof(double) = %lu\n", sizeof(double));
for(i = 0; i < 2; i++)
{
printf("&da[%d] = %p (hex) = %ld (dec)\n", i, &da[i], (long)&da[i]);
}
printf("sizeof(long double) = %lu\n", sizeof(long double));
for(i = 0; i < 2; i++)
{
printf("&lda[%d] = %p (hex) = %ld (dec)\n", i, &lda[i], (long)&lda[i]);
}
return 0;
}
/*
gcc arraysaddresses.c -o arraysaddresses
./arraysaddresses
sizeof(char) = 1
&ca[0] = 0x7ffcf40cc496 (hex) = 140724402963606 (dec)
&ca[1] = 0x7ffcf40cc497 (hex) = 140724402963607 (dec)
sizeof(short) = 2
&sa[0] = 0x7ffcf40cc42c (hex) = 140724402963500 (dec)
&sa[1] = 0x7ffcf40cc42e (hex) = 140724402963502 (dec)
sizeof(int) = 4
&ia[0] = 0x7ffcf40cc430 (hex) = 140724402963504 (dec)
&ia[1] = 0x7ffcf40cc434 (hex) = 140724402963508 (dec)
sizeof(long) = 8
&la[0] = 0x7ffcf40cc440 (hex) = 140724402963520 (dec)
&la[1] = 0x7ffcf40cc448 (hex) = 140724402963528 (dec)
sizeof(long long) = 8
&lla[0] = 0x7ffcf40cc450 (hex) = 140724402963536 (dec)
&lla[1] = 0x7ffcf40cc458 (hex) = 140724402963544 (dec)
sizeof(float) = 4
&fa[0] = 0x7ffcf40cc438 (hex) = 140724402963512 (dec)
&fa[1] = 0x7ffcf40cc43c (hex) = 140724402963516 (dec)
sizeof(double) = 8
&da[0] = 0x7ffcf40cc460 (hex) = 140724402963552 (dec)
&da[1] = 0x7ffcf40cc468 (hex) = 140724402963560 (dec)
sizeof(long double) = 16
&lda[0] = 0x7ffcf40cc470 (hex) = 140724402963568 (dec)
&lda[1] = 0x7ffcf40cc480 (hex) = 140724402963584 (dec)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char ca[2]; short sa[2]; // arrays
int i, ia[2]; long la[2]; long long lla[2];
float fa[2]; double da[2]; long double lda[2];
cout << "sizeof(char) = "<< sizeof(char) << endl;
for(i = 0; i < 2; i++)
{
cout << "&ca[" << i << "] = " << static_cast<void *>(&ca[i])
<< " (hex) = " << (long)&ca[i] << " (dec)" << endl;
}
cout << "sizeof(short) = "<< sizeof(short) << endl;
for(i = 0; i < 2; i++)
{
cout << "&sa[" << i << "] = " << &sa[i] << " (hex) = "
<< (long)&sa[i] << " (dec)" << endl;
}
cout << "sizeof(int) = "<< sizeof(int) << endl;
for(i = 0; i < 2; i++)
{
cout << "&ia[" << i << "] = " << &ia[i] << " (hex) = "
<< (long)&ia[i] << " (dec)" << endl;
}
cout << "sizeof(long) = "<< sizeof(long) << endl;
for(i = 0; i < 2; i++)
{
cout << "&la[" << i << "] = " << &la[i] << " (hex) = "
<< (long)&la[i] << " (dec)" << endl;
}
cout << "sizeof(long long) = "<< sizeof(long long) << endl;
for(i = 0; i < 2; i++)
{
printf("&lla[%d] = %p (hex) = %ld (dec)\n", i, &lla[i], (long)&lla[i]);
}
cout << "sizeof(float) = "<< sizeof(float) << endl;
for(i = 0; i < 2; i++)
{
cout << "&fa[" << i << "] = " << &fa[i] << " (hex) = "
<< (long)&fa[i] << " (dec)" << endl;
}
cout << "sizeof(double) = "<< sizeof(double) << endl;
for(i = 0; i < 2; i++)
{
cout << "&da[" << i << "] = " << &da[i] << " (hex) = "
<< (long)&da[i] << " (dec)" << endl;
}
cout << "sizeof(long double) = "<< sizeof(long double) << endl;
for(i = 0; i < 2; i++)
{
cout << "&lda[" << i << "] = " << &lda[i] << " (hex) = "
<< (long)&lda[i] << " (dec)" << endl;
}
return 0;
}
/*
g++ ArraysAddresses.cpp -o ArraysAddresses
./ArraysAddresses
sizeof(char) = 1
&ca[0] = 0x7ffe2c4a6236 (hex) = 140729641493046 (dec)
&ca[1] = 0x7ffe2c4a6237 (hex) = 140729641493047 (dec)
sizeof(short) = 2
&sa[0] = 0x7ffe2c4a61cc (hex) = 140729641492940 (dec)
&sa[1] = 0x7ffe2c4a61ce (hex) = 140729641492942 (dec)
sizeof(int) = 4
&ia[0] = 0x7ffe2c4a61d0 (hex) = 140729641492944 (dec)
&ia[1] = 0x7ffe2c4a61d4 (hex) = 140729641492948 (dec)
sizeof(long) = 8
&la[0] = 0x7ffe2c4a61e0 (hex) = 140729641492960 (dec)
&la[1] = 0x7ffe2c4a61e8 (hex) = 140729641492968 (dec)
sizeof(long long) = 8
&lla[0] = 0x7ffe2c4a61f0 (hex) = 140729641492976 (dec)
&lla[1] = 0x7ffe2c4a61f8 (hex) = 140729641492984 (dec)
sizeof(float) = 4
&fa[0] = 0x7ffe2c4a61d8 (hex) = 140729641492952 (dec)
&fa[1] = 0x7ffe2c4a61dc (hex) = 140729641492956 (dec)
sizeof(double) = 8
&da[0] = 0x7ffe2c4a6200 (hex) = 140729641492992 (dec)
&da[1] = 0x7ffe2c4a6208 (hex) = 140729641493000 (dec)
sizeof(long double) = 16
&lda[0] = 0x7ffe2c4a6210 (hex) = 140729641493008 (dec)
&lda[1] = 0x7ffe2c4a6220 (hex) = 140729641493024 (dec)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-20. Apply the technique in ArrayAddresses.cpp (see ch3-ArrayAddresses) to print out the size of the struct and the addresses of the array elements in StructArray.cpp.
#include <stdio.h> // for printf(), putchar()
#include <string.h> // for strcat(), strcpy()
typedef struct
{
int i, j, k;
} ThreeDpoint;
void print3d(ThreeDpoint);
void print3dAddresses(ThreeDpoint*, char*);
void reverse(char* s, int len);// reverse `s', knowing its length
int itoa (int , char*); // int to string of chars
int main()
{
char name[20], index[10]; // p[i]
ThreeDpoint p[2];
printf("sizeof(int) = %lu\n", sizeof(int));
printf("sizeof(ThreeDpoint) = %lu\n", sizeof(ThreeDpoint));
printf("sizeof(p) = %lu\n", sizeof(p));
printf("&p = %p (hex) = %ld (dec)\n", &p, (long)&p);
putchar('\n');
for(int i = 0; i < 2; i++) // local i
{
printf("sizeof(p[%d]) = %lu\n", i, sizeof(p[i])); // local i
print3d(p[i]); // local i
p[i].i = i + 1; // local i, struct i, local i
p[i].j = i + 2; // local i, struct j, local i
p[i].k = i + 3; // local i, struct k, local i
print3d(p[i]); // local i
putchar('\n');
strcpy(name, "p["); // (re)set name
itoa(i, index); // int to string
strcat(name, index); // concatenation
strcat(name, "]"); // concatenation
print3dAddresses(&p[i], name); // pass `p[i]' by address
putchar('\n');
}
return 0;
}
void print3d(ThreeDpoint p)
{ // struct i, j, k:
printf("(%d, %d, %d), ", p.i, p.j, p.k);
}
void print3dAddresses(ThreeDpoint* p, char* name)
{ // struct i, j, k:
printf("&%s = %p (hex) = %ld (dec)\n", name, &(*p), (long)&(*p));
printf("&%s.i = %p (hex) = %ld (dec)\n", name, &p->i, (long)&p->i);
printf("&%s.j = %p (hex) = %ld (dec)\n", name, &p->j, (long)&p->j);
printf("&%s.k = %p (hex) = %ld (dec)\n", name, &p->k, (long)&p->k);
}
void reverse(char* s, int len)// reverse `s', knowing its length
{
int i = 0, j = len-1;
char temp;
while (i < j)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
// get the digits of n into `s' (convert n to a string of characters),
// return the length of `s' (no of digits, plus an eventual sign):
int itoa (int n, char* s)
{
int i = 0, sign = 1;
if (n < 0)
{
sign = -1;
}
do
{ // get the digits in reverse order:
s[i++] = (n % 10)*sign + '0'; // get next (last read) digit
// if n < 0, (n /= 10) <= 0, (n % 10) < 0
} while (n /= 10); // while ((n /= 10) != 0); // delete last digit
if (sign < 0) {s[i++] = '-';} // first char after reverse
s[i] = '\0'; // end the string
reverse(s, i);
return i;
}
/*
gcc struct_array_addresses.c -o struct_array_addresses
./struct_array_addresses
sizeof(int) = 4
sizeof(ThreeDpoint) = 12
sizeof(p) = 24
&p = 0x7ffca9810e40 (hex) = 140723152293440 (dec)
sizeof(p[0]) = 12
(194, 0, -1451159945), (1, 2, 3),
&p[0] = 0x7ffca9810e40 (hex) = 140723152293440 (dec)
&p[0].i = 0x7ffca9810e40 (hex) = 140723152293440 (dec)
&p[0].j = 0x7ffca9810e44 (hex) = 140723152293444 (dec)
&p[0].k = 0x7ffca9810e48 (hex) = 140723152293448 (dec)
sizeof(p[1]) = 12
(32764, -1451159946, 32764), (2, 3, 4),
&p[1] = 0x7ffca9810e4c (hex) = 140723152293452 (dec)
&p[1].i = 0x7ffca9810e4c (hex) = 140723152293452 (dec)
&p[1].j = 0x7ffca9810e50 (hex) = 140723152293456 (dec)
&p[1].k = 0x7ffca9810e54 (hex) = 140723152293460 (dec)
*/
In function print3dAddresses() we have to pass p[i] by address (a pointer) to be able to print addresses (by default a struct is passed by value, see Kernighan_and_Ritchie, Chapter_6).
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <string>
using std::string;
using std::to_string;
using std::cout;
using std::endl;
typedef struct
{
int i, j, k;
} ThreeDpoint;
void print3d(ThreeDpoint);
void print3dAddresses(ThreeDpoint&, string);
int main()
{
ThreeDpoint p[2];
cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(ThreeDpoint) = " << sizeof(ThreeDpoint) << endl;
cout << "sizeof(p) = " << sizeof(p) << endl;
cout << "&p = " << &p << " (hex) = " << (long)&p << " (dec)" << endl;
cout << endl;
for(int i = 0; i < 2; i++) // local i
{
cout << "sizeof(p[" << i << "]) = " << sizeof(p[i]) << endl; // local i
print3d(p[i]); // local i
p[i].i = i + 1; // local i, struct i, local i
p[i].j = i + 2; // local i, struct j, local i
p[i].k = i + 3; // local i, struct k, local i
print3d(p[i]); // local i
cout << endl;
string name = "p[" + to_string(i) + "]"; // concatenation
print3dAddresses(p[i], name); // pass `p[i]' by reference
cout << endl;
}
return 0;
}
void print3d(ThreeDpoint p)
{ // struct i, j, k:
cout << "(" << p.i << ", " << p.j << ", " << p.k << "), ";
}
void print3dAddresses(ThreeDpoint& p, string name)
{ // struct i, j, k:
cout << "&" << name << " = " << &p << " (hex) = "
<< (long)&p << " (dec)" << endl;
cout << "&" << name << ".i = " << &p.i << " (hex) = "
<< (long)&p.i << " (dec)" << endl;
cout << "&" << name << ".j = " << &p.j << " (hex) = "
<< (long)&p.j << " (dec)" << endl;
cout << "&" << name << ".k = " << &p.k << " (hex) = "
<< (long)&p.k << " (dec)" << endl;
}
/*
g++ StructArrayAddresses.cpp -o StructArrayAddresses
./StructArrayAddresses
sizeof(int) = 4
sizeof(ThreeDpoint) = 12
sizeof(p) = 24
&p = 0x7ffd3e0cc350 (hex) = 140725644477264 (dec)
sizeof(p[0]) = 12
(893293288, 32550, 891547175), (1, 2, 3),
&p[0] = 0x7ffd3e0cc350 (hex) = 140725644477264 (dec)
&p[0].i = 0x7ffd3e0cc350 (hex) = 140725644477264 (dec)
&p[0].j = 0x7ffd3e0cc354 (hex) = 140725644477268 (dec)
&p[0].k = 0x7ffd3e0cc358 (hex) = 140725644477272 (dec)
sizeof(p[1]) = 12
(32550, -907271855, 21939), (2, 3, 4),
&p[1] = 0x7ffd3e0cc35c (hex) = 140725644477276 (dec)
&p[1].i = 0x7ffd3e0cc35c (hex) = 140725644477276 (dec)
&p[1].j = 0x7ffd3e0cc360 (hex) = 140725644477280 (dec)
&p[1].k = 0x7ffd3e0cc364 (hex) = 140725644477284 (dec)
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int a[10];
cout << "a = " << a << endl;
cout << "&a[0] = " << &a[0] << endl;
return 0;
}
/*
g++ ArrayIdentifier.cpp -o ArrayIdentifier
./ArrayIdentifier
a = 0x7ffe8f201b70
&a[0] = 0x7ffe8f201b70
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), putchar()
void printArray(int a[], char* name, int size);
int main()
{
int i, a[5];
int* ip = a;
for(i = 0; i < 5; i++)
{ip[i] = i * 10;}
printArray(a, "a", 5);
printArray(ip, "ip", 5); // a
// a = 0; // compile error: assignment to expression with array type
ip = 0; // null pointer
printArray(a, "a", 5); // not changed
// printArray(ip, "ip", 5); // run-time error: Segmentation fault (core dumped)
int b[5];
// a = b; // compile error: assignment to expression with array type
ip = b;
for(i = 0; i < 5; i++)
{ip[i] = i * 20;}
printArray(a, "a", 5); // not changed
printArray(b, "b", 5);
printArray(ip, "ip", 5); // b
return 0;
}
void printArray(int a[], char* name, int size)
{
int i;
for(i = 0; i < size; i++)
{printf("%s[%d] = %d, ", name, i, a[i]);}
putchar('\n');
}
/*
gcc pointers_brackets.c -o pointers_brackets
./pointers_brackets
a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40,
ip[0] = 0, ip[1] = 10, ip[2] = 20, ip[3] = 30, ip[4] = 40,
a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40,
a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40,
b[0] = 0, b[1] = 20, b[2] = 40, b[3] = 60, b[4] = 80,
ip[0] = 0, ip[1] = 20, ip[2] = 40, ip[3] = 60, ip[4] = 80,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
void printArray(int a[], string name, int size);
int main()
{
int a[5];
int* ip = a;
for(int i = 0; i < 5; i++)
{ip[i] = i * 10;}
printArray(a, "a", 5);
printArray(ip, "ip", 5); // a
// a = 0; // compile error: incompatible types in assignment of int to int[]
ip = 0; // null pointer
printArray(a, "a", 5); // not changed
// printArray(ip, "ip", 5); // run-time error: Segmentation fault (core dumped)
int b[5];
// a = b; // compile error: invalid array assignment
ip = b;
for(int i = 0; i < 5; i++)
{ip[i] = i * 20;}
printArray(a, "a", 5); // not changed
printArray(b, "b", 5);
printArray(ip, "ip", 5); // b
return 0;
}
void printArray(int a[], string name, int size)
{
for(int i = 0; i < size; i++)
{cout << name << "[" << i << "] = " << a[i] << ", ";}
cout << endl;
}
/*
g++ PointersAndBrackets.cpp -o PointersAndBrackets
./PointersAndBrackets
a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40,
ip[0] = 0, ip[1] = 10, ip[2] = 20, ip[3] = 30, ip[4] = 40,
a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40,
a[0] = 0, a[1] = 10, a[2] = 20, a[3] = 30, a[4] = 40,
b[0] = 0, b[1] = 20, b[2] = 40, b[3] = 60, b[4] = 80,
ip[0] = 0, ip[1] = 20, ip[2] = 40, ip[3] = 60, ip[4] = 80,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), putchar()
void func1(int a[], int size); // virtually identical
void func2(int* a, int size); // argument lists
void print(int a[], char name[], int size); // virtually identical
void print(int* a, char* name, int size); // function declarations
int main()
{
int a[5], b[5];
// Probably garbage values:
print(a, "a", 5);
print(b, "b", 5);
putchar('\n');
// Initialize the arrays:
func1(a, 5);
func1(b, 5);
print(a, "a", 5);
print(b, "b", 5);
putchar('\n');
// Notice the arrays are always modified:
func2(a, 5); // array name is a pointer
func2(b, 5); // (passed by reference or address, not by value)
print(a, "a", 5);
print(b, "b", 5);
return 0;
}
void func1(int a[], int size)
{
int i;
for(i = 0; i < size; i++)
{a[i] = i * i - i;}
}
void func2(int* a, int size)
{
int i;
for(i = 0; i < size; i++)
{a[i] = i * i + i;}
}
void print(int a[], char* name, int size)
{
int i;
for(i = 0; i < size; i++)
{printf("%s[%d] = %d, ", name, i, a[i]);}
putchar('\n');
}
/* // compile error: redefinition of `print' (overloading not allowed in C):
void print(int* a, char name[], int size)
{
int i;
for(i = 0; i < size; i++)
{printf("%s[%d] = %d, ", name, i, a[i]);}
putchar('\n');
}
*/
/*
gcc arrayarguments.c -o arrayarguments
./arrayarguments
a[0] = -1251763802, a[1] = 32767, a[2] = 1701077981, a[3] = 21962, a[4] = 72315624,
b[0] = 0, b[1] = 0, b[2] = 1701077152, b[3] = 21962, b[4] = -1251763536,
a[0] = 0, a[1] = 0, a[2] = 2, a[3] = 6, a[4] = 12,
b[0] = 0, b[1] = 0, b[2] = 2, b[3] = 6, b[4] = 12,
a[0] = 0, a[1] = 2, a[2] = 6, a[3] = 12, a[4] = 20,
b[0] = 0, b[1] = 2, b[2] = 6, b[3] = 12, b[4] = 20,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
void func1(int a[], int size); // virtually identical
void func2(int* a, int size); // argument lists
void print(int a[], string name, int size); // compiler considers these two
void print(int* a, string name, int size); // function declarations identical
int main()
{
int a[5], b[5];
// Probably garbage values:
print(a, "a", 5);
print(b, "b", 5);
cout << endl;
// Initialize the arrays:
func1(a, 5);
func1(b, 5);
print(a, "a", 5);
print(b, "b", 5);
cout << endl;
// Notice the arrays are always modified:
func2(a, 5); // array name is a pointer
func2(b, 5); // (passed by reference or address, not by value)
print(a, "a", 5);
print(b, "b", 5);
return 0;
}
void func1(int a[], int size)
{
for(int i = 0; i < size; i++)
{a[i] = i * i - i;}
}
void func2(int* a, int size)
{
for(int i = 0; i < size; i++)
{a[i] = i * i + i;}
}
// compiler registers the signature print(int*, string, int):
void print(int a[], string name, int size)
{
for(int i = 0; i < size; i++)
{cout << name << "[" << i << "] = " << a[i] << ", ";}
cout << endl;
}
/* // compile error: redefinition of function print(int*, string, int):
void print(int* a, string name, int size)
{
for(int i = 0; i < size; i++)
{cout << name << "[" << i << "] = " << a[i] << ", ";}
cout << endl;
}
*/
/*
g++ ArrayArguments.cpp -o ArrayArguments
./ArrayArguments
a[0] = 808467128, a[1] = 32767, a[2] = 1, a[3] = 0, a[4] = 808466800,
b[0] = 640, b[1] = 0, b[2] = 65535, b[3] = 1, b[4] = 808466816,
a[0] = 0, a[1] = 0, a[2] = 2, a[3] = 6, a[4] = 12,
b[0] = 0, b[1] = 0, b[2] = 2, b[3] = 6, b[4] = 12,
a[0] = 0, a[1] = 2, a[2] = 6, a[3] = 12, a[4] = 20,
b[0] = 0, b[1] = 2, b[2] = 6, b[3] = 12, b[4] = 20,
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 3-21. Create an array of string objects and assign a string to each element. Print out the array using a for() loop.
#include <stdio.h> // for printf(), putchar()
void print(char* a[], int size); // virtually identical
void print(char** a, int size); // function declarations
int main()
{
char* a[5] = {"Hello!", "What's", "up?"};
print(a, 5); // prints extra (null) and space for 2 elements
print(a, 3);
// char* b[5] = {"How", "are", "you", "today?", ''}; // error: '' not string
char* b[5] = {"How", "are", "you", "today?", ""}; // "" is a string
print(b, 5); // prints extra space
char* c[5] = {"I", "am", "fine,", "thank", "you!", ""}; // warning:
print(c, 5); // excess elements in array initializer (compiles just fine)
// print(c, 6); // compiles just fine, prints nothing, run-time errors:
// print(c, 7); // Segmentation fault (core dumped)
return 0;
}
void print(char* a[], int size) // char** a
{
int i;
for(i = 0; i < size; i++)
{printf("%s ", a[i]);}
putchar('\n');
}
/*
gcc strings.c -o strings
./strings
Hello! What's up? (null) (null)
Hello! What's up?
How are you today? // 1 extra space
I am fine, thank you!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
void print(string a[], int size); // compiler considers these two
void print(string* a, int size); // function declarations identical
int main()
{
string a[5] = {"Hello!", "What's", "up?"};
print(a, 5); // prints 2 extra spaces
print(a, 3);
// string b[5] = {"How", "are", "you", "today?", ''}; // error: '' not string
string b[5] = {"How", "are", "you", "today?", ""}; // "" is a string
print(b, 5); // prints extra space
// compile error: too many initializers for string[5]:
// string c[5] = {"I", "am", "fine,", "thank", "you!", ""};
string c[5] = {"I", "am", "fine,", "thank", "you!"};
print(c, 5); // prints a newline
// print(c, 6); // no error or warning, prints no newline
// print(c, 7); // no error or warning, runs for ever, stopped by Ctrl^C
return 0;
}
// compiler registers the signature print(string*, int):
void print(string a[], int size) // string* a
{
for(int i = 0; i < size; i++)
{cout << a[i] << " ";}
cout << endl;
}
/*
g++ Strings.cpp -o Strings
./Strings
Hello! What's up? // 2 extra spaces
Hello! What's up?
How are you today? // 1 extra space
I am fine, thank you!
// I am fine, thank you! // no newline here // print(c, 6);
// I am fine, thank you! // print(c, 7); // ./Strings | less // Ctrl^C + q
*/
print(c, 7);
you can run the program with
./Strings | less
but then you have to quickly press Ctrl^C to not run out of memory, Then you can stop the program by pressing `q' (quit).