A
arnuld
this programme gives unusual output. i am not able to find out where the
semantic bug lies:
/* C++ Primer - 4/e
*
* an example from section section 7.2.4, page 241 * STATEMENT
* write a function that prints the elements of an array. don't use
pointer to an array as parameter because pointer will be copied, use
reference to the array instead.
*
*/
#include <iostream>
/* prints the elements of an array. i could have used subscripting but the
point here is the understanding of pointers and references */
void print_array( int (&arr)[3], const size_t arr_size ) {
int* pbegin = arr;
int* pend = pbegin + arr_size;
while( pbegin != pend)
{
std::cout << *pbegin++ << std::endl;
}
}
int main()
{
const size_t arr_size = 3;
int arr_3[3] = {10, 20, 30};
print_array( (&arr_3)[3], arr_size);
return 0;
}
/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra test.cpp
~/programming/cpp $ ./a.out
-1079705144
-1210589296
1
~/programming/cpp $
*/
semantic bug lies:
/* C++ Primer - 4/e
*
* an example from section section 7.2.4, page 241 * STATEMENT
* write a function that prints the elements of an array. don't use
pointer to an array as parameter because pointer will be copied, use
reference to the array instead.
*
*/
#include <iostream>
/* prints the elements of an array. i could have used subscripting but the
point here is the understanding of pointers and references */
void print_array( int (&arr)[3], const size_t arr_size ) {
int* pbegin = arr;
int* pend = pbegin + arr_size;
while( pbegin != pend)
{
std::cout << *pbegin++ << std::endl;
}
}
int main()
{
const size_t arr_size = 3;
int arr_3[3] = {10, 20, 30};
print_array( (&arr_3)[3], arr_size);
return 0;
}
/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra test.cpp
~/programming/cpp $ ./a.out
-1079705144
-1210589296
1
~/programming/cpp $
*/