A
arnuld
this does not work, i know there is some problem in the "for loop" of
"print_arr" function. i am not able to correct the weired results i am
getting. i have no compile time error, it is only semantic-bug that is
causing the trouble:
EXPECTED: january, february, march....december
GOT: january, january, january.........january
------------- PROGRAMME --------------
/* Stroustrup, 5.9, exercise 10
STSTAMENT:
define an array of strings,where strings contains the names months
.. Print those strings. Pass the array to a function that prints those
strings.
SOLUTION:
1.) 1st, i will print array int he "main" using "for" loop
and array indexing.
2.) then i wil print he array using a function and passing the array
to the function as argument(pass by reference).
NOTICE: posted code is implementation of (2)
*/
#include<iostream>
void print_arr(const char**, size_t);
int main()
{
const char* arr[] = {"january", "february", "march", "april", "may",
"june",
"july", "august", "september", "october", "november",
"december"};
const size_t arr_size = sizeof(arr) / sizeof(*arr);
print_arr(arr, arr_size);
return 0;
}
void print_arr(const char** arr, size_t arr_size)
{
const char** p = arr;
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i)
std::cout << *p << std::endl;
}
-------------- OUTPUT ----------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]$ ./a.out
USING FUNCTION
january
january
january
january
january
january
january
january
january
january
january
january
[arch@voodo tc++pl]$
"print_arr" function. i am not able to correct the weired results i am
getting. i have no compile time error, it is only semantic-bug that is
causing the trouble:
EXPECTED: january, february, march....december
GOT: january, january, january.........january
------------- PROGRAMME --------------
/* Stroustrup, 5.9, exercise 10
STSTAMENT:
define an array of strings,where strings contains the names months
.. Print those strings. Pass the array to a function that prints those
strings.
SOLUTION:
1.) 1st, i will print array int he "main" using "for" loop
and array indexing.
2.) then i wil print he array using a function and passing the array
to the function as argument(pass by reference).
NOTICE: posted code is implementation of (2)
*/
#include<iostream>
void print_arr(const char**, size_t);
int main()
{
const char* arr[] = {"january", "february", "march", "april", "may",
"june",
"july", "august", "september", "october", "november",
"december"};
const size_t arr_size = sizeof(arr) / sizeof(*arr);
print_arr(arr, arr_size);
return 0;
}
void print_arr(const char** arr, size_t arr_size)
{
const char** p = arr;
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i)
std::cout << *p << std::endl;
}
-------------- OUTPUT ----------------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]$ ./a.out
USING FUNCTION
january
january
january
january
january
january
january
january
january
january
january
january
[arch@voodo tc++pl]$