Stroustrup 5.9, exercise 10 (using a function)

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

STATEMENT:
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]$
 
A

arnuld

Using pointer arithmetic, you forgot to increment p; use:
for(unsigned int i=0; i < arr_size; ++i,++p)

You can also use arr instead or just deference it:
for(unsigned int i=0; i < arr_size; ++i)
std::cout << arr << std::endl;



OOPS!, sorry
 
A

arnuld

OK, i have a little of improvement. what do you say?

--------- 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**);

int main()
{
const size_t MonthsInYear = 12;

const char* arr[MonthsInYear + 1] =
{"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november",
"december", '\0'};


print_arr(arr);

return 0;
}


void print_arr(const char** arr)
{
std::cout << "\n\tUSING FUNCTION\n";
for(const char** p = arr; *p != '\0' ; ++p)
std::cout << *p << std::endl;
}

--------- OUTPUT -------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra -O 5.9_ex-10-
function.cpp
[arch@voodo tc++pl]$ ./a.out

USING FUNCTION
january
february
march
april
may
june
july
august
september
october
november
december
[arch@voodo tc++pl]$
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

OK, i have a little of improvement. what do you say?

--------- 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**);

int main()
{
const size_t MonthsInYear = 12;

const char* arr[MonthsInYear + 1] =
{"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november",
"december", '\0'};

print_arr(arr);

return 0;

}

void print_arr(const char** arr)
{
std::cout << "\n\tUSING FUNCTION\n";
for(const char** p = arr; *p != '\0' ; ++p)
std::cout << *p << std::endl;

}

Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:

void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;

}
 
A

arnuld

Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:

void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;

}

this is the 1st time i have seen someone incrementing an "array",
never saw so even in any book. does it work like this:

*arr == &arr[0]
++arr == &arr[0 + 1]

?

IOW, like a pointer
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Personally I'd prefer a version where the size is passed as an
argument just to be safe, something like your first try, but also
incrementing arr:
void print_arr(const char** arr, size_t arr_size)
{
std::cout << "\n\tUSING FUNCTION\n";
for(unsigned int i=0; i < arr_size; ++i, arr++)
std::cout << *arr << std::endl;

this is the 1st time i have seen someone incrementing an "array",
never saw so even in any book. does it work like this:

*arr == &arr[0]
++arr == &arr[0 + 1]

?

IOW, like a pointer

Yes, arr[N] is the same as *(arr[0] + N) == *(arr + N).

The following might help you understand but should *never* be used in
real life, read the rest at your own risk:


Using the transitivity of addition (meaning that A+B == B+A) we can
get some pretty ugly, but valid, syntax when indexing into arrays:

So arr[N] == *(arr + N) then we apply the transitivity rule on the
left hand giving *(N + arr) and then go back to the array-form gives
N[arr].

So given array arr then 2[arr] will give the third element in arr. And
then we replace the N with a variable and we get something like this:

int main()
{
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; ++i)
std::cout << i[arr];
return 0;
}
 
D

Default User

arnuld said:
OK, i have a little of improvement. what do you say?

const char* arr[MonthsInYear + 1] =
{"january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november",
"december", '\0'};
void print_arr(const char** arr)
{
std::cout << "\n\tUSING FUNCTION\n";
for(const char** p = arr; *p != '\0' ; ++p)
std::cout << *p << std::endl;
}

I don't like the use of '\0' there. While it's technically not wrong,
as that's a legitimate null pointer constant, I think it betrays a lack
of understanding on your part.

That last element of the array is NOT a character, particularly it is
not a character like the null terminator in a character array (C-style
string).

What you want in this case for an array terminator is a null pointer.
I'd prefer the use of a plain 0, or even NULL (although many in the C++
community don't care for that macro).




Brian
 
A

arnuld

I don't like the use of '\0' there. While it's technically not wrong,
as that's a legitimate null pointer constant, I think it betrays a lack
of understanding on your part.
:-(

That last element of the array is NOT a character, particularly it is
not a character like the null terminator in a character array (C-style
string).

well, its a new thing you told :)

What you want in this case for an array terminator is a null pointer.
I'd prefer the use of a plain 0, or even NULL (although many in the C++
community don't care for that macro).

OK, 0 (zero) then
 
D

Default User

arnuld said:
well, its a new thing you told :)

That's fine. You're in a learning situation, so details can be
important.
OK, 0 (zero) then

That would be better. Whether it's better than maintaining and passing
the size is a design question.




Brian
 
J

James Kanze

this is the 1st time i have seen someone incrementing an "array",
never saw so even in any book.

There's no array in this function, just a pointer.
does it work like this:
*arr == &arr[0]
++arr == &arr[0 + 1]

IOW, like a pointer

If you look at the declaration of the function, it's a pointer.

Had he wanted to confuse you, he could have declared the
function:

void print_arr( char const* arr[], size_t size ) ...

Despite appearances, there's no array here either. There's a
special rule that says when the type of a function parameter is
declared to be array of T, the actual type is pointer to T.

And pointers can be incremented.

Never the less, I'd keep it simple:

void print_arr( char const* arr[], size_t size )
{
for ( size_t i = 0 ; i < size ; ++ i ) {
std::cout << arr[ i ] << std::endl ;
}
}

or (more idiomatic, because closer to what you do with the STL,
but perhaps less readable anyway):

void print_arr( char const** arr, size_t size )
{
char const** end = arr + size ;
while ( arr != end ) {
std::cout << *arr << std::endl ;
++ arr ;
}
}

Note, however, the ambiguity in the use between pointers and
arrays. The definition of the [] operator is a == *(a+b).
Always. You can index arrays because an array converts
implicitly to a pointer to the first element in most contexts.
And the [] is defined as a pointer operator, not an array
operator. Note that this means that things like "abcd"[ i ],
or even i[ "abcd" ] are legal (where i is an int)---if a is
legal, so is b[a]. (Obviously, putting the index in front of
the braces is only good for obfuscation. But realizing that it
is legal, and why, does help understanding the oddities of
arrays in C and in C++. And why so many people prefer
std::vector to a C style array:).)
 
J

James Kanze

On 2 Apr, 11:00, "arnuld" <[email protected]> wrote:
Yes, arr[N] is the same as *(arr[0] + N) == *(arr + N).

You mean *(&arr[0] + N), I'm sure.

I'd point out, at least, that there are in fact two curiousities
involved there. One, of course, is the fact that the indexing
operator is defined in terms of pointer arithmetic. The second
is the fact that an expression with array type converts
implicitly, and in (far too) many contexts to a pointer to the
first element.

The whole thing has been carefully designed to confuse beginners
in the language, and thus keep the rates for freelance
specialists higher. Personally, as a freelance specialist, I'm
all for it.
 
J

James Kanze

well, its a new thing you told :)

Then maybe we should back up a bit. An array is (by definition,
I think, and independantly of the language) a sequence of
elements, all having the same type. In your case, that type was
char const*, a *pointer* (to char). If the array contains
pointers, it cannot, ever, contain a character. The only reason
the compiler didn't complain when you put '\0' into the array is
because there are a lot (far too many, in fact) of implicit
conversions, which C++ inherits from C.
OK, 0 (zero) then

Which also invokes an implicit conversion. As does NULL: the
argument for NULL is that is says what you want (i.e. a special
value for a pointer). The argument against NULL that it doesn't
actually give you what it says. (G++ warns if you misuse it,
i.e. if you use it in a context where it doesn't get immediately
converted into a pointer. Which pretty much negates the main
argument against it.)

The problem is serious enough that there is a proposal to add a
"nullptr" to C++, which has been adopted by the committee for
C++09.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top