|
|
 |
 |
 |
 |
Converting to/from pointer
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!
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. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.
Lame Duck <nos @nospam.com> writes: > 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++. -- Ben Pfaff http://benpfaff.org
Lame Duck wrote: > 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?
Comp.lang.c++ is in the next town. Try it. -- <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt> <http://www.securityfocus.com/columnists/423> <http://www.aaxnet.com/editor/edit043.html> <http://kadaitcha.cx/vista/dogsbreakfast/index.html> cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com
Lame Duck wrote: > Hi Group! > I have a vector<float> variable that I need to pass to a function,
There are no such things as "vector<float>" in C. Post to a newsgroup appropriate to the language you are using. > 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.
"Lame Duck" <nos @nospam.com> wrote in message > 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[i] 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. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm
No, a pointer to a float is the same as an array of float. I need to make that into a vector. On 2 Jun 2007 at 18:30, osmium wrote:
> "Richard Heathfield" wrote: >> 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. > The function returns a pointer to a single float datum, right? So you want > to know how to put a float in a vector? ISTM if the function wanted to > return a vector of floats it would have *returned* a vector of floats.
Well C++ is an extension of C so no harm in asking here! Many people know both anyways... On 2 Jun 2007 at 18:11, Ben Pfaff wrote:
> Lame Duck <nos @nospam.com> writes: >> 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++.
Lame Duck said: > No, a pointer to a float is the same as an array of float.
No, it isn't. > I need to make that into a vector.
C doesn't have a 'vector' type. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.
On 2 Jun 2007 at 21:01, Richard Heathfield wrote: > Lame Duck said: >> No, a pointer to a float is the same as an array of float. > 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. >> I need to make that into a vector. > 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.
On 2007-06-02 14:16:23 -0700, Lame Duck <nos@nospam.com> said: > On 2 Jun 2007 at 21:01, Richard Heathfield wrote: >> Lame Duck said: >>> No, a pointer to a float is the same as an array of float. >> 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.
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) >>> I need to make that into a vector. >> C doesn't have a 'vector' type. > True, C++ does,
That doesn't make this any more on topic for comp.lang.c > though you can define extra types in C as structs - > infact a struct is essentially a class with no constructor or > destructor.
-- Clark S. Cox III clarkc@gmail.com
Lame Duck <nos @nospam.com> writes: > On 2 Jun 2007 at 21:01, Richard Heathfield wrote: >> Lame Duck said: >>> No, a pointer to a float is the same as an array of float. >> 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.
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"). >>> I need to make that into a vector. >> 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.
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. -- Ben Pfaff http://benpfaff.org
Lame Duck wrote: > 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.
Lame Duck wrote: > On 2 Jun 2007 at 21:01, Richard Heathfield wrote: >> Lame Duck said: >>> No, a pointer to a float is the same as an array of float. >> 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.
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. -- Eric Sosman esos@acm-dot-org.invalid
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. -- Flash Gordon
Martin Ambuhl wrote: > Lame Duck wrote: >> Hi Group! >> I have a vector<float> variable that I need to pass to a function, > There are no such things as "vector<float>" in C. Post to a newsgroup > appropriate to the language you are using. >> 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.
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 ... -- Eric Sosman esos@acm-dot-org.invalid
Clark Cox <clarkc @gmail.com> writes: > On 2007-06-02 14:16:23 -0700, Lame Duck <nos @nospam.com> said: >> On 2 Jun 2007 at 21:01, Richard Heathfield wrote: >>> Lame Duck said: >>>> No, a pointer to a float is the same as an array of float. >>> 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. > 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)
Why? The poster, while wrong, doesn't know who or what Richard Heathfield is.
Lame Duck <nos @nospam.com> writes: > On 2 Jun 2007 at 21:01, Richard Heathfield wrote: >> Lame Duck said: >>> No, a pointer to a float is the same as an array of float. >> 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.
A pointer is not an array. An array is not a pointer. Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. >>> I need to make that into a vector. >> 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.
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.) -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Lame Duck <nos @nospam.com> writes: > Well C++ is an extension of C so no harm in asking here! Many people > know both anyways... Wrong. C and C++ are two different languages, and questions about C++ are *not welcome* in comp.lang.c. And please don't top-post; see the following links: http://www.caliburn.nl/topposting.html http://www.cpax.org.uk/prg/writings/topposting.php Followups redirected, since this is no longer about C *or* C++. -- Keith Thompson (The_Other_Keith) k@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
Flash Gordon <s @flash-gordon.me.uk> writes: > 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.
If it started out as an extension then what is it now? An extensions of the extension? ......
On Sun, 03 Jun 2007 00:07:36 +0200, in comp.lang.c , Richard <rgr @gmail.com> wrote: >> 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.
True, but if they had followed usenet etiquette and lurked, or had googled his name, they'd have a fair idea that he probably knows a /lot/ more than the OP about C .... http://www.google.co.uk/search?q=richard+heathfield -- Mark McIntyre "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian Kernighan
In article <87d50edmkm. @gmail.com>, Richard <rgr @gmail.com> wrote: >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 -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
Mark McIntyre <markmcint @spamcop.net> writes: > On Sun, 03 Jun 2007 00:07:36 +0200, in comp.lang.c , Richard > <rgr @gmail.com> wrote: >>> 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. > True, but if they had followed usenet etiquette and lurked, or had > googled his name, they'd have a fair idea that he probably knows a > /lot/ more than the OP about C .... > http://www.google.co.uk/search?q=richard+heathfield
So, you think a poster should search up the name of everyone he replies too? I don't think so somehow. I would point out that while he was wrong, the OP was neither rude nor disrespectful. A polite correction is far more valid than a lecturing on how great one might think Richard Heathfield or another regular is.
Richard wrote, On 02/06/07 23:21: > Flash Gordon <s @flash-gordon.me.uk> writes: >> 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. > If it started out as an extension then what is it now? An extensions of > the extension? ......
Well, it could be an example of divergent evolution. If you want to discus it go over to comp.lang.c++ where C++ is topical. Follow-ups set. -- Flash Gordon
Flash Gordon <s @flash-gordon.me.uk> writes: > Richard wrote, On 02/06/07 23:21: >> Flash Gordon <s @flash-gordon.me.uk> writes: >>> 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. >> If it started out as an extension then what is it now? An extensions of >> the extension? ...... > Well, it could be an example of divergent evolution. If you want to > discus it go over to comp.lang.c++ where C++ is topical.
This is called a divergent thread. If you wish to ignore it then please do. But while the link between C and C++ is being discussed it is reasonably on topic and I'm quite sure anyone discussing does not need your permission or re-directions to other newsgroups - especially when the C++ one would be as "on or off topic" as the C one when one considers BOTH languages are being discussed.
|
 |
 |
 |
 |
|