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

Converting to/from pointer

P: n/a
Hi Group!

I have a vector<floatvariable 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!

Jun 2 '07 #1
Share this Question
Share on Google+
156 Replies

P: n/a
Lame Duck said:
Hi Group!

I have a vector<floatvariable 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.
Jun 2 '07 #2

P: n/a
Lame Duck <no****@nospam.comwrites:
I have a vector<floatvariable 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
Jun 2 '07 #3

P: n/a
Lame Duck wrote:
>
I have a vector<floatvariable 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

Jun 2 '07 #4

P: n/a
Lame Duck wrote:
Hi Group!

I have a vector<floatvariable 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.
Jun 2 '07 #5

P: n/a
"Lame Duck" <no****@nospam.comwrote in message
Hi Group!

I have a vector<floatvariable 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

Jun 2 '07 #6

P: n/a
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<floatvariable 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.

Jun 2 '07 #7

P: n/a
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 <no****@nospam.comwrites:
>I have a vector<floatvariable 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++.
Jun 2 '07 #8

P: n/a
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.
Jun 2 '07 #9

P: n/a
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.

Jun 2 '07 #10

P: n/a
On 2007-06-02 14:16:23 -0700, Lame Duck <no****@nospam.comsaid:
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
cl*******@gmail.com

Jun 2 '07 #11

P: n/a
Lame Duck <no****@nospam.comwrites:
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
Jun 2 '07 #12

P: n/a
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.
Jun 2 '07 #13

P: n/a
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
es*****@acm-dot-org.invalid
Jun 2 '07 #14

P: n/a
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
Jun 2 '07 #15

P: n/a
Martin Ambuhl wrote:
Lame Duck wrote:
>Hi Group!

I have a vector<floatvariable 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
es*****@acm-dot-org.invalid
Jun 2 '07 #16

P: n/a
Clark Cox <cl*******@gmail.comwrites:
On 2007-06-02 14:16:23 -0700, Lame Duck <no****@nospam.comsaid:
>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.
Jun 2 '07 #17

P: n/a
Lame Duck <no****@nospam.comwrites:
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) ks***@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"
Jun 2 '07 #18

P: n/a
Lame Duck <no****@nospam.comwrites:
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) ks***@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"
Jun 2 '07 #19

P: n/a
Flash Gordon <sp**@flash-gordon.me.ukwrites:
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? ......
Jun 2 '07 #20

P: n/a
On Sun, 03 Jun 2007 00:07:36 +0200, in comp.lang.c , Richard
<rg****@gmail.comwrote:
>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
Jun 2 '07 #21

P: n/a
In article <87************@gmail.com>, Richard <rg****@gmail.comwrote:
>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.
Jun 2 '07 #22

P: n/a
Mark McIntyre <ma**********@spamcop.netwrites:
On Sun, 03 Jun 2007 00:07:36 +0200, in comp.lang.c , Richard
<rg****@gmail.comwrote:
>>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.

Jun 2 '07 #23

P: n/a
Richard wrote, On 02/06/07 23:21:
Flash Gordon <sp**@flash-gordon.me.ukwrites:
>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
Jun 3 '07 #24

P: n/a
Flash Gordon <sp**@flash-gordon.me.ukwrites:
Richard wrote, On 02/06/07 23:21:
>Flash Gordon <sp**@flash-gordon.me.ukwrites:
>>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.
Jun 3 '07 #25

P: n/a
On 2007-06-02 15:07:36 -0700, Richard <rg****@gmail.comsaid:
Clark Cox <cl*******@gmail.comwrites:
>On 2007-06-02 14:16:23 -0700, Lame Duck <no****@nospam.comsaid:
>>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.
Perhaps a smiley was in order, but I was merely point out that I found
that fact amusing.

--
Clark S. Cox III
cl*******@gmail.com

Jun 3 '07 #26

P: n/a
Richard Tobin wrote:
Richard <rg****@gmail.comwrote:
>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.
No it isn't. Try using the identifiers new, class, namespace, for
example, in a C program compiled via C++. It has grown into a
language of itself. That is one reason it has its own standard.
Try using malloc without casting. etc. etc. etc.

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

Jun 3 '07 #27

P: n/a
Richard wrote:
Mark McIntyre <ma**********@spamcop.netwrites:
.... snip ...
>>
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 ....
.... snip ...
>
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.
However, if he had followed Usenet practice, and lurked for a while
before disturbing by posting, he would have a good idea.

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

Jun 3 '07 #28

P: n/a
Eric Sosman wrote:
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.
Maybe he would be better convinced by an example. Some use of
'sizeof' is probably warrented, as is use of functions outside
main. I am too lazy at the moment to generate that example.

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

Jun 3 '07 #29

P: n/a
CBFalconer wrote:
Richard Tobin wrote:
>Richard <rg****@gmail.comwrote:
>>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.

No it isn't.
Yes, it is. It is an extension of the subset of C that is legal C++.

--
Ian Collins.
Jun 3 '07 #30

P: n/a
Ian Collins wrote:
CBFalconer wrote:
>Richard Tobin wrote:
>>Richard <rg****@gmail.comwrote:

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.
No it isn't.

Yes, it is. It is an extension of the subset of C that is legal C++.
It is also an extension of the subset of Fortran that is legal C++.
Indeed, it is an extension of the subset of Tibetan that is legal C++.
Congratulations on one of the most vacuous utterances ever written.

Jun 3 '07 #31

P: n/a
Martin Ambuhl wrote:
Ian Collins wrote:
>CBFalconer wrote:
>>Richard Tobin wrote:
Richard <rg****@gmail.comwrote:

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.
No it isn't.

Yes, it is. It is an extension of the subset of C that is legal C++.

It is also an extension of the subset of Fortran that is legal C++.
Indeed, it is an extension of the subset of Tibetan that is legal C++.
Congratulations on one of the most vacuous utterances ever written.
Did you read Richard Tobin's comment?

--
Ian Collins.
Jun 3 '07 #32

P: n/a
Martin Ambuhl <ma*****@earthlink.netwrites:
Ian Collins wrote:
>CBFalconer wrote:
>>Richard Tobin wrote:
Richard <rg****@gmail.comwrote:

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.
No it isn't.
Yes, it is. It is an extension of the subset of C that is legal C++.

It is also an extension of the subset of Fortran that is legal
C++. Indeed, it is an extension of the subset of Tibetan that is legal
C++. Congratulations on one of the most vacuous utterances ever
written.
Which he already explictly acknowledged with the phrase "which of
course covers everything".

(And since the subset of C that is legal C++ is a very large fraction
of C, it's hardly vacuous in this particular case.)

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 3 '07 #33

P: n/a
I really find it hard to understand the bile and negativity that seems
to be this Group's bread and butter! I've read any number of Groups and
Forums and by and large they're all pretty friendly places, but here all
I've got is a torrent of abuse!
>>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 }; // <<--- did you mean { not (
???
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 ...
I really don't understand what this code is doing! I think you
misunderstood my question - &MyVector.front() already works, the problem
is to convert pointer/array to vector without having to copy all data by
hand.

Jun 3 '07 #34

P: n/a
On 2 Jun 2007 at 21:42, Ben Pfaff wrote:
Lame Duck <no****@nospam.comwrites:
>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").
OK, maybe in C there's some subtelty that I haven't worked out, but
definitely in C++ - one of the main "soundbite to remember" in my C++
class was "an array is just the same thing as a constant pointer".
>>>
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.
What I meant was: a C struct is like a C++ class without a constructor
or destructor.

Jun 3 '07 #35

P: n/a
Lame Duck said:
I really find it hard to understand the bile and negativity that seems
to be this Group's bread and butter!
You didn't get any bile from me. You got a polite request to take your
discussion to a group where it's relevant.
I've read any number of Groups
and Forums and by and large they're all pretty friendly places,
I'm friendly places too, but clc is about C, not C++.
but here all I've got is a torrent of abuse!
No, you got a polite request to take your discussion to a group where
it's relevant. Polite requests don't count as abuse.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 3 '07 #36

P: n/a
Lame Duck said:
On 2 Jun 2007 at 21:42, Ben Pfaff wrote:
<snip>
>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").

OK, maybe in C there's some subtelty that I haven't worked out, but
definitely in C++ - one of the main "soundbite to remember" in my C++
class was "an array is just the same thing as a constant pointer".
Well, that's wrong in C++ as well as in C. In C++, an array is *not*
just the same thing as a constant pointer. Your teacher was wrong.

<snip>
What I meant was: a C struct is like a C++ class without a constructor
or destructor.
No, it isn't. There are quite a few differences between C structs and
C++ classes. Indeed, there are quite a few differences between C
structs and C++ structs.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 3 '07 #37

P: n/a
Lame Duck wrote:
On 2 Jun 2007 at 21:42, Ben Pfaff wrote:
>Lame Duck <no****@nospam.comwrites:
>>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").

OK, maybe in C there's some subtelty that I haven't worked out, but
definitely in C++ - one of the main "soundbite to remember" in my C++
class was "an array is just the same thing as a constant pointer".
Read the FAQ entry, it applies to C++ as much as to C.

--
Ian Collins.
Jun 3 '07 #38

P: n/a
"Lame Duck" <no****@nospam.comwrote in message
>
I really find it hard to understand the bile and negativity that seems
to be this Group's bread and butter! I've read any number of Groups and
Forums and by and large they're all pretty friendly places, but here all
I've got is a torrent of abuse!
We hate C++ here because it is runing our nice language.
I just installed Java on my new Vista machine. Something actually works! It
is such a clean any easy way to get graphics up and running compared with
trying to get C sharp, C++, C++ with funny Chinese hats, or whatever, to
work. Java is a much better way to go if you want object-orientation.

Evil Microsoft have even broken C. When I tried to compile some portable
modules for a graphical app, the compiler complained about the unsafe
function strcpy(). Bye bye ANSI C, I suppose.
>
I really don't understand what this code is doing! I think you
misunderstood my question - &MyVector.front() already works, the
problem is to convert pointer/array to vector without having to copy all
data
by hand.
By hand? I know C++ is bad, but surely not.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 3 '07 #39

P: n/a
"Lame Duck" <no****@nospam.comwrote
>
What I meant was: a C struct is like a C++ class without a constructor
or destructor.
Kind of. It very helpful to know C if youa re trying to understand why C++
is the way it is.
C structs lay out variables contiguously in memory. C++ classes extend that
idea to tie functions to the data types they operate on then, and this is
the clever part, express relationships between different functions and data
types.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 3 '07 #40

P: n/a
Lame Duck <no****@nospam.comwrites:
I really find it hard to understand the bile and negativity that seems
to be this Group's bread and butter! I've read any number of Groups and
Forums and by and large they're all pretty friendly places, but here all
I've got is a torrent of abuse!
It is a strange group is comp.lang.c. far more "hostile" than the c++
equivalent too. While keeping "on topic" is valuable, there seems to be
a feeding frenzy in here amongst 9 or 10 posters who cant wait to get in
first with their warnings. I've counted about 7 "Off topic" warnings to
one post from the "usual suspects" either though each one knew another
would post the same warning.

Never quite understood it myself.
Jun 3 '07 #41

P: n/a

"Lame Duck" <no****@nospam.comha scritto nel messaggio
news:sl*******************@nospam.com...
Well C++ is an extension of C so no harm in asking here! Many people
know both anyways...
If we agreed with you, comp.lang.c++ would have never been created.
And FYI C++ isn't completely a superset of C, see:
http://www.research.att.com/~bs/bs_faq.html#C-is-subset
Jun 3 '07 #42

P: n/a

"Lame Duck" <no****@nospam.comha scritto nel messaggio
news:sl*******************@nospam.com...
No, a pointer to a float is the same as an array of float.
#include <stdio.h>
int main(void)
{
float array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float *pointer = array;

printf("%lu %lu\n", sizeof array, sizeof pointer);
return 0;
}

Displays "40 4" on my system. So? They're the same but one is ten times
bigger? :-)
Jun 3 '07 #43

P: n/a

"Martin Ambuhl" <ma*****@earthlink.netha scritto nel messaggio
news:5c*************@mid.individual.net...
Ian Collins wrote:
>CBFalconer wrote:
>>Richard Tobin wrote:
Richard <rg****@gmail.comwrote:

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.
No it isn't.

Yes, it is. It is an extension of the subset of C that is legal C++.

It is also an extension of the subset of Fortran that is legal C++.
Indeed, it is an extension of the subset of Tibetan that is legal C++.
Congratulations on one of the most vacuous utterances ever written.
At least, the subset of C that is legal C++ is larger than the
subset of Tibetan that is legal C++. (And so is the subset of Latin
which is legal (whatever it means) Italian.) Bjourne Stroustrup
claims that every example in K&R2 is valid C++.

(But the sentence above is still silly.)
Jun 3 '07 #44

P: n/a
Lame Duck wrote:
OK, maybe in C there's some subtelty that I haven't worked out, but
definitely in C++ - one of the main "soundbite to remember" in my C++
class was "an array is just the same thing as a constant pointer".
That is wrong in both C++ and C. Either your instructor was grossly in
error or you misunderstood.

Jun 3 '07 #45

P: n/a
On Sun, 3 Jun 2007 17:09:08 +0200, "Army1987" <pl********@for.it>
wrote:
>
"Lame Duck" <no****@nospam.comha scritto nel messaggio
news:sl*******************@nospam.com...
>No, a pointer to a float is the same as an array of float.

#include <stdio.h>
int main(void)
{
float array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float *pointer = array;

printf("%lu %lu\n", sizeof array, sizeof pointer);
It would be better to cast the last two arguments so someone whose
system did not #define size_t as unsigned long would not invoke
undefined behavior.
return 0;
}

Displays "40 4" on my system. So? They're the same but one is ten times
bigger? :-)
At least now we know what the original poster meant by superset.
Remove del for email
Jun 3 '07 #46

P: n/a

"Army1987" <pl********@for.itwrote in message
news:f3**********@tdi.cu.mi.it...
>
"Lame Duck" <no****@nospam.comha scritto nel messaggio
news:sl*******************@nospam.com...
>No, a pointer to a float is the same as an array of float.

#include <stdio.h>
int main(void)
{
float array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float *pointer = array;

printf("%lu %lu\n", sizeof array, sizeof pointer);
return 0;
}

Displays "40 4" on my system. So? They're the same but one is ten times
bigger? :-)
So there's a bug in the standard that breaks array pointer equivalence? It
happens to even the best languages.
Deprectate sizeof( object) and only allow sizeof(type) and the pro
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
blem is fixed.

Jun 3 '07 #47

P: n/a
Lame Duck <no****@nospam.comwrites:
I really find it hard to understand the bile and negativity that seems
to be this Group's bread and butter! I've read any number of Groups and
Forums and by and large they're all pretty friendly places, but here all
I've got is a torrent of abuse!
You have been advised repeatedly, and in many cases quite politely,
that your question is about C++, not about C, and that you should post
it in comp.lang.c++, not in comp.lang.c. And yet you persist in
posting to comp.lang.c.

You are being rude. It's not surprising that some people here in
comp.lang.c are rude to you in response. There *may* have been some
overreaction; I suggest you ignore it and move on.

It should be obvious by now that you're *not* going to get any useful
responses from comp.lang.c. I don't know what you expect to
accomplish by continuing to post here.

[...]
> 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 }; // <<--- did you mean { not (
???
> 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 ...

I really don't understand what this code is doing! I think you
misunderstood my question - &MyVector.front() already works, the problem
is to convert pointer/array to vector without having to copy all data by
hand.
Don't worry about understanding the code; the author was being
sarcastic. He was *pretending* that your question is a valid question
about C (it must have been, since you posted it to comp.lang.c).
Since C doesn't have "vectors", he invented a set of C declarations
for which "&MyVector.front()" would be valid C. The fact that the
resulting code doesn't address your problem is a result of your
posting your question to the wrong newsgroup. (He sprinkled the code
with C++ keywords used as identifiers, guaranteeing that it's not
valid C++, just to emphasize the point.)

Incidentally, I can't tell who wrote the code because you deleted the
attribution lines. Please don't do that.

Your real question, I think, is how to copy the contents of an array
into a C++ vector without copying each element individually. (I don't
know the answer to that, since I'm not sufficiently familiar with C++.
It may well be that the best solution is to copy the elements one at a
time in a loop. Or perhaps std::vector provides an operation to do
what you want in one fell swoop, though such an operation probably
works internally by looping over the elements.) You should ask *that*
question, and you should ask it in comp.lang.c++, where it's perfectly
topical. You'll probably get numerous excellent and well-informed
answers. You might even get some good advice on how to re-structure
your code so the operation you're looking for isn't needed in the
first place.

I suggest starting a new thread in comp.lang.c++, since this one's
signal-to-noise ratio has degraded considerably.

Or you can continue to waste everyone's time by whining about how rude
we've been to you. It's up to you.

--
Keith Thompson (The_Other_Keith) ks***@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"
Jun 3 '07 #48

P: n/a
On Jun 3, 1:00 am, Mark McIntyre <markmcint...@spamcop.netwrote:
On Sun, 03 Jun 2007 00:07:36 +0200, in comp.lang.c , Richard
<rgr...@gmail.comwrote:
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,
It depends. The original poster apparently cross-posted
(although his question has nothing to do with clc). You can
lurk for a long time in clc++, and not know who Richard
Heathfield is. (Another reason why cross-posting is bad.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
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

Jun 3 '07 #49

P: n/a
On 2007-06-03 13:33:20 -0700, "Malcolm McLean" <re*******@btinternet.comsaid:
>
"Army1987" <pl********@for.itwrote in message
news:f3**********@tdi.cu.mi.it...
>>
"Lame Duck" <no****@nospam.comha scritto nel messaggio
news:sl*******************@nospam.com...
>>No, a pointer to a float is the same as an array of float.

#include <stdio.h>
int main(void)
{
float array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float *pointer = array;

printf("%lu %lu\n", sizeof array, sizeof pointer);
return 0;
}

Displays "40 4" on my system. So? They're the same but one is ten times
bigger? :-)
So there's a bug in the standard that breaks array pointer equivalence?
It's not a bug, it's that way by design. Pointers and arrays have never
been equivalent.
It happens to even the best languages.
Deprectate sizeof( object) and only allow sizeof(type) and the pro
Not only would that break perfectly valid code, it wouldn't change
anything; arrays would still not be pointers:

#include <stdio.h>
int main(void)
{
printf("%lu %lu\n", (unsigned long)sizeof(float[10]), (unsigned
long)sizeof(float*));
return 0;
}

[OT in clc]
And, in C++, since you can take references to arrays, the situations in
which they are not equivalent to pointers are even greater in number.
[/OT]
--
Clark S. Cox III
cl*******@gmail.com

Jun 3 '07 #50

156 Replies

This discussion thread is closed

Replies have been disabled for this discussion.