Valarray/Pointer to first Element

C

Chris Forone

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
 
C

Christopher

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?
 
G

Guest

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.
 
Z

Zeppe

Christopher said:
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?

How would
you use it opposed to a reference?

one thing is a reference, another thing is a pointer.

Regards,

Zeppe
 
C

Chris Forone

Erik said:
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::eek:perator 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!
 
G

Guest

Erik said:
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::eek:perator 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.
 
C

Chris Forone

Erik said:
Erik said:
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::eek:perator 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.
 
J

James Kanze

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 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.
 
J

James Kanze

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::eek:perator 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.
 
C

Chris Forone

James said:
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::eek:perator 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:[email protected]
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

Thx a lot!
 
C

Chris Forone

James said:
You're wrong, of course. If you can get a reference, you can
get a pointer.


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.)



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:[email protected]
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
Thx a lot!
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top