|
From: Ramana K. <ra...@my...> - 2002-08-01 08:56:58
|
Vectors would be great, except for the fact that i can't initialise them
using {...}
which is the only convenient way to do something like
{
"YOU'RE","YOU ARE",
"I'M","I AM",
}
and i have checked that vectors can't be initialised by passing them an
array.
so, i have three options remaining.
1. this is quite inelegant, but since arrays are initialised with {...}, can
i not pass a value in {} directly to the function so that the parameter is
treated as a local array within the function?
2. i assume this is not possible, but perhaps since the function receives a
pointer to the first element of a array, i can somehow dereference this to
get the whole array or something like that?
3. this is what i will have to do soon... just write the array locally
thanks for all your help so far
ramana
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Ioannis
Vranos
Sent: Thursday, 1 August 2002 1:09 AM
To: 'Ramana Kumar'; 'C++'
Subject: RE: [Dev-C++] sizeof array
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Ramana Kumar
Sent: Wednesday, July 31, 2002 3:36 PM
To: Carlos Garc?a del Monte; C++
Subject: RE: [Dev-C++] sizeof array
thank you carlos
i had in fact thought of using a special array member at the end, and thank
you for illustrating implentation of this so beautifully.
i'm afraid this may have to be the way i do it in the end, unless i simply
don't pass the array and declare it locally.
Or use vector:
#include <iostream>
#include <vector>
void f(std::vector<std::vector<int> >&array)
{
using namespace std;
cout<<array.size()<<endl;
}
int main()
{
using namespace std;
/// 2-dimensional 100x50 int array initialised to 0
vector<vector<int> >arr(100, vector<int>(50));
f(arr);
}
but since the array is in fact a two-dimensional array full of instances of
my own String class, adding a "terminating character" will involve creating
too much arbitrary code for my liking. I am still dreaming of the
possibility of sizeof-ing the array with the built in operator, if anyone
knows how to do this correctly. In fact, i am highly curious about the
workings of the sizeof operator. I realize that since passing an array to a
function is actually passing a pointer to the first object ( is this x[0] or
x[0][0] in a multidimensional array?),
x[0] and &x[0][0]
the sizeof operator gets the size of that pointer... or does it? what are
you passing the operator when the array is local?
When the array is local and you pass the array's name you pass the array. A
function can accept only a pointer to the first object or a reference, so in
the function you pass the pointer variable (which points to the first
element of the array). As a result you get the size of the pointer variable
itself.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|