january january january january january 1月 january january january january january january [arch @ voodo tc ++ pl] $ 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 amgetting. i have no compile time error, it is only semantic-bug that iscausing the trouble: EXPECTED: january, february, march....decemberGOT: january, january, january.........january ------------- PROGRAMME --------------/* Stroustrup, 5.9, exercise 10 STATEMENT:define an array of strings,where strings contains the names months .Print those strings. Pass the array to a function that prints thosestrings. SOLUTION: 1.) 1st, i will print array int he "main" using "for" loop and arrayindexing. 2.) then i wil print he array using a function and passing the arrayto 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 FUNCTIONjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuary[arch@voodo tc++pl]$ 推荐答案 g ++ - ansi -pedantic -Wall -Wextra 5.9_ex-10- function.cpp [arch @ voodo tc ++ pl] g++ -ansi -pedantic -Wall -Wextra 5.9_ex-10-function.cpp[arch@voodo tc++pl] ./ a.out 使用功能 january january january january january january january january 1月 january january january [arch @ voodo tc ++ pl] ./a.out USING FUNCTIONjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuaryjanuary[arch@voodo tc++pl] 这篇关于Stroustrup 5.9,练习10(使用功能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 01:07