473,486 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reference to an Array of some dimension

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 $

*/

--
http://arnuld.blogspot.com

Aug 14 '07 #1
7 3390
On 2007-08-14 09:15, arnuld wrote:
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 */
I'm not so sure about that, since you are passing an array, not a
pointer to the first element, you know the number of elements in the
array, so a for-loop would be perfect.
void print_array( int (&arr)[3], const size_t arr_size ) {
You know that there there are 3 elements and the function is hard-coded
for such arrays, no need for the second argument.
int* pbegin = arr;
int* pend = pbegin + arr_size;

while( pbegin != pend)
{
std::cout << *pbegin++ << std::endl;
}
Could be replaced with

for (size_t i = 0; i < 3; ++i)
{
std::cout << arr[i] << std::endl;
}
}
int main()

{
const size_t arr_size = 3;
Drop this, or use it to set the size of the array.
int arr_3[3] = {10, 20, 30};

print_array( (&arr_3)[3], arr_size);
I'm not really sure, but I think this passes the address of the fourth
element, not the array. When passing an array by reference you do it
just like any other variable you pass by reference, with its name:

print_array(arr_3);

--
Erik Wikström
Aug 14 '07 #2
On Tue, 14 Aug 2007 07:47:13 +0000, Erik Wikström wrote:
>arnuld wrote:
I'm not so sure about that, since you are passing an array, not a
pointer to the first element, you know the number of elements in the
array, so a for-loop would be perfect.
i know but it produces error:

for( int* pbegin = arr, pend = pbegin + arr_size;
pbegin != pend; ++pbegin)
{
std::cout << *pbegin++ << std::endl;
}
7.2.4_print-array.cpp: In function 'void print_array(int (&)[3],
size_t)': 7.2.4_print-array.cpp:23: error: invalid conversion from
'int*' to 'int'

7.2.4_print-array.cpp:23: error: ISO C++ forbids comparison between
pointer and integer

i did not understand why it was taking "pend" as integer. and page 117 of
C++ Primer says: i am stupid because i wrote that: "int* pbegin, pend"
thing and expected both of them to be a pointer, in fact they are 2
different types"

>void print_array( int (&arr)[3], const size_t arr_size ) {

You know that there there are 3 elements and the function is hard-coded
for such arrays, no need for the second argument.
actually, i wanted it to be like this: "int (&arr)[]" but that is
compile time error :(

7.2.4_print-array.cpp:18: error: parameter 'arr' includes reference
to array of unknown bound 'int []'

for (size_t i = 0; i < 3; ++i)
{
std::cout << arr[i] << std::endl;
}
yes, it is easy and the author wants me to use pointers, arrays and
references.

> print_array( (&arr_3)[3], arr_size);
I'm not really sure, but I think this passes the address of the fourth
element, not the array. When passing an array by reference you do it
just like any other variable you pass by reference, with its name:

print_array(arr_3);
and i was passing, print_array(&arr_3[]), heck....references always
confuse me.
BTW, here is the new code, compiles and runs fine and th eonly thing i
wanted to implement was passing a reference to an array of unknown array
(Aye.. do i need Dynamic Array for this ?)

/* 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 as parameter.
*
*/
#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 ) {
for( int *pbegin = arr, *pend = pbegin + arr_size; pbegin != pend;
++pbegin)
{
std::cout << *pbegin << std::endl;
}

}
int main()

{
const size_t arr_size = 3;
int arr_3[arr_size] = {10, 20, 30};

print_array( arr_3, arr_size);

return 0;
}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
7.2.4_print-array.cpp ~/programming/cpp $ ./a.out
10
20
30
~/programming/cpp $

*/
--
http://arnuld.blogspot.com

Aug 14 '07 #3
arnuld wrote:
>On Tue, 14 Aug 2007 07:47:13 +0000, Erik Wikström wrote:
>>arnuld wrote:
>I'm not so sure about that, since you are passing an array, not a
pointer to the first element, you know the number of elements in the
array, so a for-loop would be perfect.

i know but it produces error:

for( int* pbegin = arr, pend = pbegin + arr_size;
pbegin != pend; ++pbegin)
{
std::cout << *pbegin++ << std::endl;
}
7.2.4_print-array.cpp: In function 'void print_array(int (&)[3],
size_t)': 7.2.4_print-array.cpp:23: error: invalid conversion from
'int*' to 'int'

7.2.4_print-array.cpp:23: error: ISO C++ forbids comparison between
pointer and integer

i did not understand why it was taking "pend" as integer. and page 117 of
C++ Primer says: i am stupid because i wrote that: "int* pbegin, pend"
thing and expected both of them to be a pointer, in fact they are 2
different types"
Seems to me that you are unaware of how variable declarations work in C++.
int* a,b;
declares a as a pointer to int, and b as a pointer. This is because
declarations are split in two syntax elements: The type and the declarator
list. Here, the type is int, and the declarator list is *a and b, and the
declarations says: "*a" and "b" should be recognised as ints. That's why it
is common to write
int *a, b
instead. It demonstrates to the reader that the asterisk belongs to a only.
Of course, the clearer and advisable solution is to write two declarations.

In your for loop, write:
for (int* iter = arr; iter < arr + arr_size; ++iter) {
std::cout << *iter << std::endl; //not *iter++.
}

I chose another name than pbegin for the iterator, it does only once point
to the beginning of the array. The compiler should be smart enough to
compute arr + arr_size only once, you don't need to cache this value
yourself.
>>void print_array( int (&arr)[3], const size_t arr_size ) {

You know that there there are 3 elements and the function is hard-coded
for such arrays, no need for the second argument.

actually, i wanted it to be like this: "int (&arr)[]" but that is
compile time error :(
Yes, it's a compile time error. C++ has to know how large the array is to
reference it.
BTW, here is the new code, compiles and runs fine and th eonly thing i
wanted to implement was passing a reference to an array of unknown array
Cannot be done. But you could use templates to let the compiler do the work
of figuring out the array sizes.
/* 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 as parameter.
*
*/
#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 ) {
for( int *pbegin = arr, *pend = pbegin + arr_size; pbegin != pend;
++pbegin)
{
std::cout << *pbegin << std::endl;
}

}
#include <cstddef// for std::size_t

/* template solution */
template <std::size_t arr_size>
void print_array(int (&arr)[arr_size]) {
// ...
}

call it with

print_array(arr_3);
int main()

{
const size_t arr_size = 3;
int arr_3[arr_size] = {10, 20, 30};

print_array( arr_3, arr_size);

return 0;
}

/* OUTPUT
~/programming/cpp $ g++ -ansi -pedantic -Wall -Wextra
7.2.4_print-array.cpp ~/programming/cpp $ ./a.out
10
20
30
~/programming/cpp $

*/

--
rbh
Aug 14 '07 #4
Hi!

arnuld schrieb:
i did not understand why it was taking "pend" as integer. and page 117 of
C++ Primer says: i am stupid because i wrote that: "int* pbegin, pend"
thing and expected both of them to be a pointer, in fact they are 2
different types"
Yes, two types: pinter and int. The "*" does not count for the second
variable.
actually, i wanted it to be like this: "int (&arr)[]" but that is
compile time error :(
Yes, there is no reference to an array of unknown size. That just
doesn't exist. That's why everywhere you see "int* array, int size" as
arguments ("int" as size, although it should be "size_t", but they
aren't using const neither).

"The C++ way" of doing it is to use "iterators". In this case they would
just be pointers to int:

void print_int_array(int const* const pbegin, int const* const pend)
{
while(pbegin != pend)
cout << *pbegin++ << '\n';
}

This can be made to work with other thing that aren't an array. You need
templates for that. I guess you are not that far yet in your book, so
I'll skip the example.

Frank
Aug 14 '07 #5
On Aug 14, 3:06 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
arnuld schrieb:
actually, i wanted it to be like this: "int (&arr)[]" but that is
compile time error :(
Yes, there is no reference to an array of unknown size.
That's what g++ says, but I can't find the restriction in the
standard. Where does the standard says that something like:

extern int arr[] ;

void
f( int (&a)[] )
{
}

void
g()
{
f( arr ) ;
}

Is forbidden?

What is true is that unlike incomplete class types, an array of
unknown size is considered a distinct type. So you can't pass
an array of known size to f, above. (Which in turn makes
parameters like that of f, above, a bit useless.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 14 '07 #6
On Aug 14, 9:15 am, arnuld <geek.arn...@gmail.comwrote:
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;
}
}
Since the function isn't using iterators, I'd use the much more
natural:

for ( int i = 0 ; i < 3 ; ++ i ) {
std::cout << arr[ i ] << std::endl ;
}

It is an array, after all.
int main()
{
const size_t arr_size = 3;
int arr_3[3] = {10, 20, 30};
print_array( (&arr_3)[3], arr_size);
And what is (&arr_3)[3] supposed to mean, here? You take the
address of the array, so you have an int(*)[3]; a pointer to an
array of three ints. Then you use the [] operator to
dereference it; accessing the fourth array of three int's.
Except that since there is only one, you have undefined
behavior.

The function print_array takes a reference to an array of three
ints. You have an array of three ints. What more do you need?
return 0;
}
FWIW: this idiom is really only useful when the function is a
template, e.g.:

template< size_t N >
void
print_array( int const (&arr)[ N ] )
{
for ( size_t i = 0 ; i < N ; ++ i ) {
std::cout << arr[ i ] << std::endl ;
}
}

Done this way, it accepts all sizes, with the compiler
calculating the size each time.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 14 '07 #7
Hi!

James Kanze schrieb:
On Aug 14, 3:06 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
>Yes, there is no reference to an array of unknown size.

That's what g++ says, but I can't find the restriction in the
standard. Where does the standard says that something like:
[code snip]
Is forbidden?
I ran Comeau:

Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions

"ComeauTest.c", line 4: error: parameter type involves reference to
array of unknown
bound
f( int (&a)[] )
^

I ran EDG/C++ on Dinkumware: It compiles. But on the following:

extern int arr[] ;

void
f( int (&a)[] )
{
}

void
g()
{
f( arr ) ;
}

int arr[] = {2, 3};

int main() {} //to shut up linker

The website went down. I don't know if that is my fault, though. o_O

No, it's back online and I reran the example:

sourceFile.cpp(11) : warning C4048: different array subscripts : 'int
(*)[]' and 'int (*)[2]'

So it's just a warning?!

Frank
Aug 14 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13998
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
6
2729
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
3
119601
by: Ohmu | last post by:
Hi! How to pass an (multidimensional)array of something to a function with reference/pointer? Can anyone help me with that? Thanks, Ohmu
4
1901
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I...
11
4429
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
4
4318
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
104
16845
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
10
12161
by: | last post by:
I'm fairly new to ASP and must admit its proving a lot more unnecessarily complicated than the other languages I know. I feel this is because there aren't many good official resources out there to...
9
3730
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.