By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,590 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

Valarray/Pointer to first Element

P: n/a
Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.

HAND Chris
Nov 2 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
On Nov 2, 10:43 am, Chris Forone <4...@gmx.atwrote:
Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.

HAND Chris
Correct me if I am wrong, but I don't believe any std container has
methods to give you a pointer to any of thier contents, and rightly
so. You can get a reference to it. Why would you want a pointer to the
first element? What type are you assuming its elements are? How would
you use it opposed to a reference?

Nov 2 '07 #2

P: n/a
On 2007-11-02 16:43, Chris Forone wrote:
Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.
&val[0];

Where val is a valarray.
--
Erik Wikström
Nov 2 '07 #3

P: n/a
Christopher wrote:
On Nov 2, 10:43 am, Chris Forone <4...@gmx.atwrote:
>Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.

HAND Chris

Correct me if I am wrong, but I don't believe any std container has
methods to give you a pointer to any of thier contents, and rightly
so. You can get a reference to it.
you can get the address of all the elements.
Why would you want a pointer to the
first element?
why not? For example, to apply the std library algorithms. A pointer to
an element of a valarray is a random-access iterator.
What type are you assuming its elements are?
well, for example, for a valarray<double>, I would expect double. :)
How would
you use it opposed to a reference?
one thing is a reference, another thing is a pointer.

Regards,

Zeppe
Nov 2 '07 #4

P: n/a
Erik Wikström schrieb:
On 2007-11-02 16:43, Chris Forone wrote:
>Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.

&val[0];

Where val is a valarray.

This solution i have already. I have seen on a Website:

valarray::operator T *

operator T *();
operator const T *() const;

Both member functions return a pointer to the first element of the
controlled array, which must have at least one element.

But i dont know, how to apply this operator...

Thx!
Nov 2 '07 #5

P: n/a
On 2007-11-02 17:35, Chris Forone wrote:
Erik Wikström schrieb:
>On 2007-11-02 16:43, Chris Forone wrote:
>>Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.

&val[0];

Where val is a valarray.

This solution i have already. I have seen on a Website:

valarray::operator T *

operator T *();
operator const T *() const;

Both member functions return a pointer to the first element of the
controlled array, which must have at least one element.

But i dont know, how to apply this operator...
Use static_cast:

double* p = static_cast<double*>(val);

where val is a valarray of doubles with at least one element.

--
Erik Wikström
Nov 2 '07 #6

P: n/a
Erik Wikström schrieb:
On 2007-11-02 17:35, Chris Forone wrote:
>Erik Wikström schrieb:
>>On 2007-11-02 16:43, Chris Forone wrote:
Hello Group,

there is some memberfunc for std::valarray to return a pointer to the
first element in the array. How do i use this? Thanx a lot.
&val[0];

Where val is a valarray.

This solution i have already. I have seen on a Website:

valarray::operator T *

operator T *();
operator const T *() const;

Both member functions return a pointer to the first element of the
controlled array, which must have at least one element.

But i dont know, how to apply this operator...

Use static_cast:

double* p = static_cast<double*>(val);

where val is a valarray of doubles with at least one element.
g++ (3.4.2) means invalid static_cast, but before i use reinterpret_cast
i will use &valarr[0] instead...

Thx.
Nov 2 '07 #7

P: n/a
On Nov 2, 5:27 pm, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
Christopher wrote:
On Nov 2, 10:43 am, Chris Forone <4...@gmx.atwrote:
there is some memberfunc for std::valarray to return a
pointer to the first element in the array. How do i use
this? Thanx a lot.
Correct me if I am wrong, but I don't believe any std
container has methods to give you a pointer to any of thier
contents, and rightly so. You can get a reference to it.
You're wrong, of course. If you can get a reference, you can
get a pointer.
you can get the address of all the elements.
Of any of the elements. (At least, I think that's what you
mean; "all of the elements" is ambiguous, and can mean two
different things.)
Why would you want a pointer to the
first element?
why not? For example, to apply the std library algorithms. A
pointer to an element of a valarray is a random-access
iterator.
I think that valarray is guaranteed to be continuous. As is
std::vector, and (soon) std::basic_string. That's not true for
other containers, however, and a pointer to a given element is
not generally a random-access iterator into the container.
(Again, I think you know this, and were only refering to
valarray, but it's not clear.)

The most frequent need for a pointer is, of course, interfacing
with legacy code or with C. In which case, the type of the
container is constrained; if the C code expects a pointer to the
first element, you can use vector, I think valarray, and in
practice basic_string, but nothing else.

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

Nov 3 '07 #8

P: n/a
On Nov 2, 5:35 pm, Chris Forone <4...@gmx.atwrote:
Erik Wikstrm schrieb:On 2007-11-02 16:43, Chris Forone wrote:
there is some memberfunc for std::valarray to return a
pointer to the first element in the array. How do i use
this? Thanx a lot.
&val[0];
Where val is a valarray.
This solution i have already. I have seen on a Website:
valarray::operator T *
operator T *();
operator const T *() const;
Which Web site? It's not in the standard. In general, the
authors of the standard avoided implicit conversions.
Correctly---there are really very few cases where they dont'
cause problems.
Both member functions return a pointer to the first element of
the controlled array, which must have at least one element.
Neither member function exists.

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

Nov 3 '07 #9

P: n/a
James Kanze schrieb:
On Nov 2, 5:35 pm, Chris Forone <4...@gmx.atwrote:
>Erik Wikstrm schrieb:On 2007-11-02 16:43, Chris Forone wrote:
>>>there is some memberfunc for std::valarray to return a
pointer to the first element in the array. How do i use
this? Thanx a lot.
>> &val[0];
>>Where val is a valarray.
>This solution i have already. I have seen on a Website:
>valarray::operator T *
>operator T *();
operator const T *() const;

Which Web site? It's not in the standard. In general, the
authors of the standard avoided implicit conversions.
Correctly---there are really very few cases where they dont'
cause problems.
publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/standlib/ref/i26lt3bvalarray26gt3b.htm
>
>Both member functions return a pointer to the first element of
the controlled array, which must have at least one element.

Neither member function exists.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Thx a lot!
Nov 3 '07 #10

P: n/a
James Kanze schrieb:
On Nov 2, 5:27 pm, Zeppe <ze...@remove.all.this.long.comment.yahoo.it>
wrote:
>Christopher wrote:
>>On Nov 2, 10:43 am, Chris Forone <4...@gmx.atwrote:
>>>there is some memberfunc for std::valarray to return a
pointer to the first element in the array. How do i use
this? Thanx a lot.
>>Correct me if I am wrong, but I don't believe any std
container has methods to give you a pointer to any of thier
contents, and rightly so. You can get a reference to it.

You're wrong, of course. If you can get a reference, you can
get a pointer.
>you can get the address of all the elements.

Of any of the elements. (At least, I think that's what you
mean; "all of the elements" is ambiguous, and can mean two
different things.)
>>Why would you want a pointer to the
first element?
>why not? For example, to apply the std library algorithms. A
pointer to an element of a valarray is a random-access
iterator.

I think that valarray is guaranteed to be continuous. As is
std::vector, and (soon) std::basic_string. That's not true for
other containers, however, and a pointer to a given element is
not generally a random-access iterator into the container.
(Again, I think you know this, and were only refering to
valarray, but it's not clear.)

The most frequent need for a pointer is, of course, interfacing
with legacy code or with C. In which case, the type of the
container is constrained; if the C code expects a pointer to the
first element, you can use vector, I think valarray, and in
practice basic_string, but nothing else.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
Thx a lot!
Nov 3 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.