Converting to/from pointer

L

Lame Duck

Hi Group!

I have a vector<float> variable that I need to pass to a function, but
the function takes a float * arguement. That's OK, I can convert by
doing &MyVector.front(), but when I get back a float * from the
function, how to convert that back to a vector?

Thanks in advance!
 
B

Ben Pfaff

Lame Duck said:
I have a vector<float> variable that I need to pass to a function, but
the function takes a float * arguement. That's OK, I can convert by
doing &MyVector.front(), but when I get back a float * from the
function, how to convert that back to a vector?

Your question is about C++, but this newsgroup talks about C.
Try comp.lang.c++.
 
R

Richard Heathfield

Lame Duck said:
Hi Group!

I have a vector<float> variable that I need to pass to a function, but
the function takes a float * arguement. That's OK, I can convert by
doing &MyVector.front(), but when I get back a float * from the
function, how to convert that back to a vector?

This is a C group. Ask in comp.lang.c++.

Crossposted to that group, and followups set.
 
M

Martin Ambuhl

Lame said:
Hi Group!

I have a vector<float> variable that I need to pass to a function,

but
the function takes a float * arguement.
(Note that "argument" is this spelt. Compilers will pick nits at least
as finely as I have.)
That's OK, I can convert by
doing &MyVector.front(),

No, you can't. There are is such thing as "MyVector.front()" in C. Post
to a newsgroup appropriate to the language you are using.
but when I get back a float * from the
function, how to convert that back to a vector?
Thanks in advance!

Bite me.
 
M

Malcolm McLean

Lame Duck said:
Hi Group!

I have a vector<float> variable that I need to pass to a function, but
the function takes a float * arguement. That's OK, I can convert by
doing &MyVector.front(), but when I get back a float * from the
function, how to convert that back to a vector?
You know how many floats you have.

ptr is a float, iterating with i over the array you get back.

Though it is a long time since I used C++, there will be a way of
constructing an empty vector and pushing floats into it. So simply do that.

It is huge fiddle, and illustrates one of the main problems with inventing
basic types, like structures to hold arrays of real numbers. Unless everyone
uses the type religously, it becomes a real hassle to fit pieces of code
together. The logic is usually trivial, but messy and error prone.
 
L

Lame Duck

No, a pointer to a float is the same as an array of float. I need to
make that into a vector.
 
L

Lame Duck

Well C++ is an extension of C so no harm in asking here! Many people
know both anyways...
 
L

Lame Duck

Lame Duck said:


No, it isn't.

Yes, a pointer can actually point to a whole block of float (aka array)
although in a sense it actually points to one float. What happens is
that really the first float and the top of the block of floats is the
same address, and that's where the pointer points.
C doesn't have a 'vector' type.

True, C++ does, though you can define extra types in C as structs -
infact a struct is essentially a class with no constructor or
destructor.
 
C

Clark Cox

Yes, a pointer can actually point to a whole block of float (aka array)
although in a sense it actually points to one float.

Full stop. A pointer to a float is not the same thing as an array of
float. In many circumstances, arrays are *converted* to pointers to
their initial element, but they are not the same thing.
What happens is
that really the first float and the top of the block of floats is the
same address, and that's where the pointer points.

I find it hilarious that you feel the need to tell Richard Heathfield
that. (hint, he knows full well what happens)
True, C++ does,

That doesn't make this any more on topic for comp.lang.c
 
F

Flash Gordon

Lame Duck wrote, On 02/06/07 21:41:
Well C++ is an extension of C

No it isn't. It might have started out that way but that is not what it
is now.
> so no harm in asking here!

Yes there is. You've reduced your chances of getting help drastically.

In any case, a lot of people here probably know about a lot of other
things that are not topical here, that does not make this an appropriate
place to ask about car maintenance, soldering or lots of other things.
> Many people
know both anyways...

Since those that know both and hang out in both groups may well now
decide not to help you even if you post to the correct group.
On 2 Jun 2007 at 18:11, Ben Pfaff wrote:

Please don't top post. Your reply belongs after or interspersed with the
text you are replying to. See the posts you replied to, this post, and
in fact most of the rest of the posts on this group for examples.
 
B

Ben Pfaff

Lame Duck said:
Yes, a pointer can actually point to a whole block of float (aka array)
although in a sense it actually points to one float. What happens is
that really the first float and the top of the block of floats is the
same address, and that's where the pointer points.

It is true that there is a close relationship between arrays and
pointers in C (and C++). But it is incorrect to claim that a
pointer to a float and an array of float are the same thing. The
C FAQ has a whole category of questions and answers on this topic
(section 6, "Arrays and Pointers").
True, C++ does, though you can define extra types in C as structs -
infact a struct is essentially a class with no constructor or
destructor.

You seem to have become confused about what language you are
talking about. Your statement is not true in C, as a C struct is
not a class with no constructor or destructor: C doesn't have
classes or constructors or destructors, so the statement is
meaningless. Your statement is also not true in C++, as a C++
struct can have constructors and a destructor.
 
M

Martin Ambuhl

Lame said:
Well C++ is an extension of C so no harm in asking here! Many people
know both anyways...

C++ and C are different languages. They have different syntax. They
have different semantics. They have different libraries. The only real
harm from what you wrote above is that it makes it author appear a fool.
 
E

Eric Sosman

Lame said:
Yes, a pointer can actually point to a whole block of float (aka array)
although in a sense it actually points to one float. What happens is
that really the first float and the top of the block of floats is the
same address, and that's where the pointer points.

Many of the questions in Section 6 of the comp.lang.c
Frequently Asked Questions (FAQ) list http://c-faq.com/ are
devoted to explaining why you are wrong, R-O-N-G, wrong.

If you would like to explain the finer points of C to
the FAQ maintainer so he can correct this blunder that is
so clear to you, you may be able to make some money by
selling tickets to the debate. I'm sure I'm not the only
one who could use a good laugh.
 
E

Eric Sosman

Martin said:
(Note that "argument" is this spelt. Compilers will pick nits at least
as finely as I have.)


No, you can't. There are is such thing as "MyVector.front()" in C.

Don't give up so easily!

void private(void) {
extern int class(void);
struct new {
int new;
int delete;
int (*front)(void);
} MyVector = ( 1, 2, class };
int cin = 42;
int cout = cin

&MyVector.front(),

-1;
}

Perfectly legal C, reproducing the Lame Duck's construct right
down to the ampersand and comma. It might be criticized on
stylistic grounds, but, hey ...
 
R

Richard

Clark Cox said:
Full stop. A pointer to a float is not the same thing as an array of
float. In many circumstances, arrays are *converted* to pointers to
their initial element, but they are not the same thing.


I find it hilarious that you feel the need to tell Richard Heathfield
that. (hint, he knows full well what happens)

Why? The poster, while wrong, doesn't know who or what Richard
Heathfield is.
 
K

Keith Thompson

Lame Duck said:
Yes, a pointer can actually point to a whole block of float (aka array)
although in a sense it actually points to one float. What happens is
that really the first float and the top of the block of floats is the
same address, and that's where the pointer points.

A pointer is not an array. An array is not a pointer.

Read section 6 of the comp.lang.c FAQ said:
True, C++ does, though you can define extra types in C as structs -
infact a struct is essentially a class with no constructor or
destructor.

Once again, you were asking about vectors. C doesn't have vectors.
C++ does. Questions about vectors belong in comp.lang.c++, not in
comp.lang.c.

Why are you still here? I mean, if you wanted to talk about C, you'd
be in the right place, but in that case I'd suggest starting a new
thread.

(I see that this discussion is inappropriately cross-posted to
comp.lang.c and comp.lang.c++. Richard Heathfield *tried* to redirect
followups to comp.lang.c++, but somebody overrode that. Followups
redirected again.)
 
R

Richard

Flash Gordon said:
Lame Duck wrote, On 02/06/07 21:41:

No it isn't. It might have started out that way but that is not what
it is now.

If it started out as an extension then what is it now? An extensions of
the extension? ......
 
R

Richard Tobin

If it started out as an extension then what is it now? An extensions of
the extension? ......

An "extended subset" perhaps, which of course covers everything...

-- Richard
 

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

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top