|
From: Ioannis V. <no...@ho...> - 2002-08-01 10:02:07
|
> -----Original Message-----
> From: Ramana Kumar [mailto:ra...@my...]
> Sent: Thursday, August 01, 2002 11:57 AM
> To: Ioannis Vranos; C++
> Subject: RE: [Dev-C++] sizeof array
>
>
> 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.
Of course you can:
#include <vector>
#include <string>
int main()
{
using namespace std;
char temparray[4][20]={"YOU'RE","YOU ARE", "I'M","I AM"};
vector<string>array(&temparray[0], &temparray[4]);
}
> 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?
You must understand something. Objects (including arrays) are passed by
reference only by using a pointer or reference. In the case of an built
in array it is a pointer or reference too its first element.
sizeof() does not see an array since an array is not passed, a variable
pointer pointing to the first element is passed. In case of C-style
strings you have not such a problem since there is '\0' and strlen().
But why mess with this C-style low level (and unecessary) stuff?
> 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?
By dereferencing it you get the value of the pointed element (in your
example of the char).
> 3. this is what i will have to do soon... just write the array locally
> thanks for all your help so far
Or use standard library facilities. :)
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|