469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

Converting to/from pointer

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
156 4684
On Jun 3, 11:22 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Lame Duck" <nos...@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.
If you're interested in the history, yes. As they currently
stand, C and C++ are two very different languages, with very
different idioms.
C structs lay out variables contiguously in memory.
Except when it doesn't. Both C and C++ allow padding. (C++, of
course, allows rearrangement as well, in certain cases.)
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.
C++ classes do a lot more than that (although that is an
important part). In particular, C++ classes have constructors
and a destructor, a concept completely foreign to C.

--
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 #51
On Jun 3, 10:33 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Army1987" <please....@for.itwrote in message
news:f3**********@tdi.cu.mi.it...
"Lame Duck" <nos...@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?
There is no array pointer equivalence in C++. There wasn't in
C, either, back when I was programming in C (in the 1980's), and
I'd be very surprised if they'd added one since.
It
happens to even the best languages.
Deprectate sizeof( object) and only allow sizeof(type) and the pro
I don't think that sizeof( float[10] ) will be equal to sizeof(
float* ), either.

Arrays and pointers in C++ work exactly as they did in C90.
Unless C99 radically changed this in the language (which would
really surprise me), arrays and pointers are two different
things, just as they are in C++.

And array will convert to a pointer in certain circumstances.
But then, a double will also convert to an int in certain
circumstances. An array is no more a pointer, however, than a
double is an int.

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

Malcolm McLean wrote in message...
[snip]
We hate C++ here because it is runing our nice language.
"runing"? <GLMAO.

[snip]
I know C++ is bad, but surely not.
You do know that this is cross-posted to 'comp.lang.c++', don't you?
That makes your post a 'troll'!! You trying to start a flame-war? <G>

--
Bob <GR
POVrookie
Jun 3 '07 #53
Keith Thompson said:

<snip>
It should be obvious by now that you're *not* going to get any useful
responses from comp.lang.c.
He's had plenty of useful responses from comp.lang.c - not our fault if
he can't recognise them as such.
I don't know what you expect to accomplish by continuing to post here.
He might learn more about C. He'll certainly learn more about
comp.lang.c.

<snip>

--
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 #54
On Sun, 03 Jun 2007 01:17:14 +0200, in comp.lang.c , Richard
<rg****@gmail.comwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:

So, you think a poster should search up the name of everyone he replies
too? I don't think so somehow.
Here's the situation: a self-confessed novice posts a question.
Someone answers him, but he's confused by the answer. Does he say to
himself

a) hmm,I'm a novice, I have no clue, I'll reread those posts and see
what I missed.
or
b) hmm, I know have no clue but I'll still write a correction, cos I
don't like admitting I'm lost.
>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.
Imagine someone asked a question about relativity, and Albert Einstein
answered. Imagine if the questioner then started to correct Einstein's
answer based on his own (possibly incorrect) understanding. Wouldn't
that be hilarious?
--
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 3 '07 #55
James Kanze said:
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
No, he didn't. He posted to clc. I tried to move the whole thing over to
clc++ by crossposting it there and setting followups, but someone
removed my followup suggestion.

<snip>
You can lurk for a long time in clc++, and not know who Richard
Heathfield is.
Strange but true.

--
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 #56
Keith Thompson wrote:
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.
Why do people keep responding to this idiot? It's pretty obvious he's
trolling at this point, whether or not he was merely clueless in the
beginning.

Killfile or ignore and move on.


Brian
Jun 3 '07 #57
Malcolm McLean wrote, On 03/06/07 21:33:
>
"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?
This is not a bug in the standard, just one example of pointers and
arrays being fundamentally different things.
It happens to even the best languages.
Deprectate sizeof( object) and only allow sizeof(type) and the pro
So you want to deprecate something which makes it easier to program
correctly because you don't like the way the language works. If you
don't like C or C++ find another language.
--
Flash Gordon
Jun 3 '07 #58
Somehow I doubt that - my instructor was a C++ expert who'd programmed
for more than one very big company.

On 3 Jun 2007 at 16:22, Martin Ambuhl wrote:
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 #59
On 3 Jun 2007 at 20:33, Keith Thompson wrote:
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.
No I'm posting this now in 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.)
Well it only served to confuse not enlighten, let me assure you.
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've asked that in this thread, but got no answer except the one you
suggested of copying every element manually. The whole point was to try
to avoid having to do that!
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.
Jun 3 '07 #60
Lame Duck wrote:

Don't top-post.

On 3 Jun 2007 at 16:22, Martin Ambuhl wrote:
>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.
Somehow I doubt that - my instructor was a C++ expert who'd programmed
for more than one very big company.
He was still wrong.

--
Ian Collins.
Jun 3 '07 #61
Lame Duck wrote:
On 3 Jun 2007 at 20:33, Keith Thompson wrote:
>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.

No I'm posting this now in comp.lang.c++.
Then why did you cross-post? This time, observe the follow-up.

--
Ian Collins.
Jun 3 '07 #62
Lame Duck <no****@nospam.comwrites:
On 3 Jun 2007 at 20:33, Keith Thompson wrote:
>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.

No I'm posting this now in comp.lang.c++.
....and to comp.lang.c too. That's really not necessary.
--
Ben Pfaff
http://benpfaff.org
Jun 3 '07 #63
[Top-posting fixed]

Lame Duck said:
On 3 Jun 2007 at 16:22, Martin Ambuhl wrote:
>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.
Somehow I doubt that - my instructor was a C++ expert who'd programmed
for more than one very big company.
Nevertheless, either he was mistaken or you misunderstood him. No matter
what his expertise, the language specification always trumps an
instructor.
--
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 #64
Mark McIntyre <ma**********@spamcop.netwrites:
On Sun, 03 Jun 2007 01:17:14 +0200, in comp.lang.c , Richard
<rg****@gmail.comwrote:
>>Mark McIntyre <ma**********@spamcop.netwrites:

So, you think a poster should search up the name of everyone he replies
too? I don't think so somehow.

Here's the situation: a self-confessed novice posts a question.
Someone answers him, but he's confused by the answer. Does he say to
himself
You are obviously paranoid and hanging around in this NG for too long
has altered you for the worst.

You clearly thought he should know who Richard Heathfield is.

This is a help group. People pop in for help and leave. They are under
no obligation to hang around to discover that Flash Gordon and
CBFalconer keep a running tally on how many times they can tell people
they are "off topic".
>
a) hmm,I'm a novice, I have no clue, I'll reread those posts and see
what I missed.
or
b) hmm, I know have no clue but I'll still write a correction, cos I
don't like admitting I'm lost.
It read more to me like he really thought he knew the right answer. It
happens.
>
>>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.

Imagine someone asked a question about relativity, and Albert Einstein
answered. Imagine if the questioner then started to correct Einstein's
answer based on his own (possibly incorrect) understanding. Wouldn't
that be hilarious?
No. It wouldn't. I would feel a little embarassed for the poster and
explain to him in other words how he is wrong and for why. And as I
said, the OP was neither rude nor disrespectful.

Richard Heathfield and yourself, are not Albert Einstein equivalents in
any shape or form. Sorry if you were lead to believe you were the
programming world's equivalent from posturing too long in this NG.

regards

Richard.

Jun 3 '07 #65
"Malcolm McLean" <re*******@btinternet.comwrites:
"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
Is that a joke?

It's not a bug. There is no equivalence between arrays and pointers,
nor is any such equivalence intended.

IMHO, there are some problems with the standard, but those are things
make it *appear* that arrays and pointers are equivalent.

We have enough trouble telling newbies that arrays and pointers are
entirely distinct concepts. Please don't add to the confusion.

--
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 #66
Lame Duck <no****@nospam.comwrites:
On 3 Jun 2007 at 20:33, Keith Thompson wrote:
[...]
>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.

No I'm posting this now in comp.lang.c++.
[...]

You are posting to both comp.lang.c and comp.lang.c++.

Read your newsreader's documentation and learn how to set the
Newsgroups: header properly.

Followups redirected to comp.lang.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 3 '07 #67
On Jun 3, 11:48 pm, Richard Heathfield <r...@see.sig.invalidwrote:
James Kanze said:
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
No, he didn't. He posted to clc. I tried to move the whole thing over to
clc++ by crossposting it there and setting followups, but someone
removed my followup suggestion.
Worse, then. If he'd have lurked for even the shortest time, he
should have noticed that std::vector wasn't an approved topic in
clc:-).

--
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 #68
On Jun 4, 12:56 am, Ian Collins <ian-n...@hotmail.comwrote:
Lame Duck wrote:
On 3 Jun 2007 at 16:22, Martin Ambuhl wrote:>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.
Somehow I doubt that - my instructor was a C++ expert who'd programmed
for more than one very big company.
He was still wrong.
There was an or in the statement he was responding to. Given
the way he is responding here, I wouldn't be too surprised if
the second clause of the or held.

--
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 #69
Lame Duck ignorantly top-posted:
Somehow I doubt that - my instructor was a C++ expert who'd programmed
for more than one very big company.
There were two choices
>That is wrong in both C++ and C. Either your instructor was grossly
in error or you misunderstood.
Your "Somehow I doubt that" is completely clueless. If your instructor
was a C++ expert, then you misunderstood. If you did not misunderstand,
then your instructor was not a C++ expert. Your "Somehow I doubt that"
has no antecedent for "that". You cannot sanely doubt the choice.
On 3 Jun 2007 at 16:22, Martin Ambuhl wrote:
>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 #70
Lame Duck wrote:
>
.... snip ...
>
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.
That code was basically a spoof. The point is that there is no
such thing as a vector in the C language. You have posted to the
wrong news group. Almost all C/C++ cross-posts are wrong.

F'ups set.

--
<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 4 '07 #71
Army1987 wrote:
>
.... snip ...
>
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.)
Certainly not if you apply the errata, which eliminates the casting
of malloc :-)

--
<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 4 '07 #72
On Sun, 03 Jun 2007 02:16:03 +0200, Richard <rg****@gmail.comwrote
in comp.lang.c:
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
[snip]

No, it is not. The reason that it is not is that the C standard does
not define such a link. The C standard did not permit or condone the
adoption of certain keywords, operators, and functions from C into
C++. Or Java, Javascript, C#, Objective C, or however many other
"borrowers" there are.

The C standard mentions C++ exactly twice, in non-normative footnotes
added at the request of the C++ standard committee.
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.
comp.lang.c is an unmoderated group, so no discussion needs anyone's
permission. But off-topic is off-topic, and any discussion of a link
between C and C++ is indeed off-topic.

C++ defines a link to C. The reverse is not true.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 4 '07 #73
CBFalconer wrote:
Army1987 wrote:
... snip ...

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

Certainly not if you apply the errata, which eliminates the casting
of malloc :-)
Even without that, it's not. K&R2 still uses implicit return types for
functions returning int, including every main().


Brian
Jun 4 '07 #74
Jack Klein <ja*******@spamcop.netwrites:
[...]
The C standard mentions C++ exactly twice, in non-normative footnotes
added at the request of the C++ standard committee.
[...]

Quibble: There are four such footnotes, plus the ban on predefining
the macro __cplusplus. This doesn't refute your point, of course.

--
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 4 '07 #75
On 3 Jun, 09:30, Lame Duck <nos...@nospam.comwrote:
On 2 Jun 2007 at 21:42, Ben Pfaff wrote:
Lame Duck <nos...@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".
how many times do you have to be told you are wrong before you
accept that you *are* wrong? Perhaps you could read the FAQ or
do some research? Read the standard perhaps?

>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
yes but a C struct is not a C++ class, by any stretch of the
imagination.

If you had an array of floats then your original question is trivial.
If you have a vector of floats then go and ask the question in
comp.lang.c++.

Why are you using floats rather than doubles?
--
Nick Keighley

Jun 4 '07 #76
On Mon, 04 Jun 2007 01:23:53 +0200, in comp.lang.c , Richard
<rg****@gmail.comwrote:
>You are obviously paranoid and hanging around in this NG for too long
has altered you for the worst.
Feel free to be offensive, its cheap and sounds impressive.
>You clearly thought he should know who Richard Heathfield is.
Actually I didn't. I did however think it was amusing that he
'corrected' him.
>This is a help group. People pop in for help and leave.
Since when did "asking for help" include correcting the advice you
were given? Do you normally correct your teacher?
>They are under
no obligation to hang around to discover that Flash Gordon and
CBFalconer keep a running tally on how many times they can tell people
they are "off topic"
Apparently the concept of nettiquette has completely passed you by.
>It read more to me like he really thought he knew the right answer. It
happens.
Absolutely. And I still maintain that its probably wiser for novices
to do some background checking on a topic and their teachers, before
shooting from the hip.
>Richard Heathfield and yourself, are not Albert Einstein equivalents in
any shape or form. Sorry if you were lead to believe you were the
programming world's equivalent from posturing too long in this NG.
Don't be a bigger prat than you need to be. If you can't understand a
metaphor when it bites you on the toe....
--
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 4 '07 #77
Nick Keighley said:
On 3 Jun, 09:30, Lame Duck <nos...@nospam.comwrote:
<snip>
>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".

how many times do you have to be told you are wrong before you
accept that you *are* wrong?
Being told many times that one is wrong does not mean that one *is*
wrong. I've been told many times that I'm wrong, by people who don't
know C. Does that mean I must accept that I'm wrong? No, of course not.
A worse case: Marilyn vos Savant was told loudly and often by *very
skilled and experienced mathematicians* that her solution to the Monty
Hall problem was wrong - but she was still right. One of those very
mathematicians used much the same argument that you did - "how many
mathematicians must tell you you are wrong before you accept that you
are wrong?" (Undoubtedly a misquote, but a fair paraphrase
nonetheless).

Lame Duck is not wrong *because* lots of people have said so in Usenet.
He's wrong because the Standard says so.
Perhaps you could read the FAQ or
do some research? Read the standard perhaps?
A much better suggestion!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 4 '07 #78
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. C is an "extended subset" of LISP, for sufficiently
large values of "extended" and sufficiently small values of "subset".

Ditto for any pair of languages.

(Note Mr. Tobin's use of "which of course covers everything".)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jun 4 '07 #79
Mark McIntyre wrote:
Since when did "asking for help" include correcting the advice you
were given? Do you normally correct your teacher?
Perhaps not normally, but sometimes the teacher is wrong (not implying
that it was the case here, just as a general statement). I have on
occasion when I was at school corrected my teacher simply because he was
wrong. It does happen.
Jun 4 '07 #80
In article <5c*************@mid.individual.net>,
Martin Ambuhl <ma*****@earthlink.netwrote:
>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.
I think you overstate the case. It is not literally true that an array
is the same as a constant pointer, but the statement does capture the
most distinctive aspect of C's array mechanism as compared with that of
other programming languages.

(And even an uncritical student who never looks into the details of the
statement will in fact, if they use C regularly, know the most
important exception to it.)

The student who wrongly believes that a C array is "just the same
thing as a constant pointer" is better educated than one who merely
knows the mechanics of using C arrays.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 4 '07 #81
In article <Qt*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>Nevertheless, either he was mistaken or you misunderstood him. No matter
what his expertise, the language specification always trumps an
instructor.
Only if your aim is to have a comprehensive graps of the details of C.
I don't know what kind of class the poster was taking, but that aim
should be rather low on the list of priorities for a university
course.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 4 '07 #82

"Richard Tobin" <ri*****@cogsci.ed.ac.ukwrote in message
news:f4**********@pc-news.cogsci.ed.ac.uk...
In article <Qt*********************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>Nevertheless, either he was mistaken or you misunderstood him. No matter
what his expertise, the language specification always trumps an
instructor.

Only if your aim is to have a comprehensive graps of the details of C.
I don't know what kind of class the poster was taking, but that aim
should be rather low on the list of priorities for a university
course.
In this case the instructor is probably simplifying for pedagogical
purposes. You have to do that. You don't throw quantum physics at nine year
olds, but you can show them models made out of balls and sticks. One problem
is that they often get the idea that atoms are coloured. However that's
better than thinking that atoms are something that only clever people can
know anything about.

However university students are there to learn the principles of
programming, not to churn out industrial spec code. As long as they know
that there is a standard, and that if you adhere to it the code will be
portable

<RANT>
Except to Microsoft's new platform. They have broken my entire code base.
</RANT>

then that's all that really needs to be learned.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 4 '07 #83
ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <5c*************@mid.individual.net>,
Martin Ambuhl <ma*****@earthlink.netwrote:
>>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.

I think you overstate the case. It is not literally true that an array
is the same as a constant pointer,
A synonym for "not literally true" is "not true".
but the statement does capture the
most distinctive aspect of C's array mechanism as compared with that of
other programming languages.
I strongly disagree. The distinctive aspect of C's array mechanism is
that *operations* on arrays are defined in terms of operations on
pointers.

Arrays are not pointers. Pointers are not arrays. Students who fail
to understand this are likely to make mistakes like:

void foo(int array[])
{
... sizeof array ...
/* expected to be the size of an array object, but it's really
the size of a pointer object */
}
(And even an uncritical student who never looks into the details of the
statement will in fact, if they use C regularly, know the most
important exception to it.)
The most important exception is that arrays are not pointers.

If you mean the most important exception to the "rule" that you can
use pointers as if they were arrays, there are several exceptions, all
of them important.
The student who wrongly believes that a C array is "just the same
thing as a constant pointer" is better educated than one who merely
knows the mechanics of using C arrays.
And the student who understands the truth is far better educated that
either of them.

The truth about arrays and pointers *isn't that hard to understand*.
It's only difficult because some instructors try to "simplify" it.

My standard response to anyone who's confused about this is:

Arrays are not pointers. Pointers are not arrays. Read section 6
of the comp.lang.c FAQ, <http://www.c-faq.com>.

Is that really so difficult that it's necessary to lie to students?

--
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 4 '07 #84
On Jun 5, 9:29 am, rich...@cogsci.ed.ac.uk (Richard Tobin) wrote:
The student who wrongly believes that a C array is "just the same
thing as a constant pointer" is better educated than one who merely
knows the mechanics of using C arrays.
I don't see how you can claim this. Say we
have an array of 100 ints on a typical system.

You think it is 'better educated' to believe
that an object of 400 bytes is the same thing
as an object of 4 bytes?

IMHO the student who knows correct information
about C arrays is better educated than one who
knows correct information and also some
dangerous untruths.

The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays. How many times a week
do we get people posting asking why they can't
assign their array of arrays to a char** ?

Jun 4 '07 #85
On Mon, 04 Jun 2007 16:06:34 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>A synonym for "not literally true" is "not true".
Point of information: this is itself not literally true.

Something not literally true can be figuratively true, or essentially
true, or largely true.

For example it is not /literally/ true that Chris Torek is a guru. At
least, I assume he's not a sikh or buddhist teacher.
(i make no comment on the pointer discussion, I'm just correcting an
incorrect correction....)
--
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 4 '07 #86

"Old Wolf" <ol*****@inspire.net.nzwrote in message
The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays. How many times a week
do we get people posting asking why they can't
assign their array of arrays to a char** ?
But multi-dimensional arrays in C are just hopeless.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 4 '07 #87
On Tue, 5 Jun 2007 00:33:34 +0100, in comp.lang.c , "Malcolm McLean"
<re*******@btinternet.comwrote:
>
"Old Wolf" <ol*****@inspire.net.nzwrote in message
>The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays.
But multi-dimensional arrays in C are just hopeless.
Eh? They've worked fine for me for 20 years. Elucidate.

--
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 4 '07 #88

"Mark McIntyre" <ma**********@spamcop.netwrote in message
news:ls********************************@4ax.com...
On Mon, 04 Jun 2007 16:06:34 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>>A synonym for "not literally true" is "not true".

Point of information: this is itself not literally true.

Something not literally true can be figuratively true, or essentially
true, or largely true.
but not true.
Jun 4 '07 #89

"Mark McIntyre" <ma**********@spamcop.netwrote in message
news:pk********************************@4ax.com...
On Tue, 5 Jun 2007 00:33:34 +0100, in comp.lang.c , "Malcolm McLean"
<re*******@btinternet.comwrote:
>>
"Old Wolf" <ol*****@inspire.net.nzwrote in message
>>The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays.
But multi-dimensional arrays in C are just hopeless.

Eh? They've worked fine for me for 20 years. Elucidate.
The problem is that you very rarely know the dimensions of a 2d array at
compile time, or if you do hardcoding it introduces an artificial fixed
limit to the program.
With 1d arrays it is not a problem since you can simply call malloc() or, in
certain cases, declare a "big enough" array. That's a lot harder with 2D
arrays.
Then finally the syntax for passing a 2D array to a function is too complex.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jun 5 '07 #90
On 2007-06-04 16:33:34 -0700, "Malcolm McLean" <re*******@btinternet.comsaid:
>
"Old Wolf" <ol*****@inspire.net.nzwrote in message
>The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays. How many times a week
do we get people posting asking why they can't
assign their array of arrays to a char** ?
But multi-dimensional arrays in C are just hopeless.
How so? They seem to work fine for me :)

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

Jun 5 '07 #91
On Jun 5, 1:00 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
"Mark McIntyre" <markmcint...@spamcop.netwrote in message

news:pk********************************@4ax.com... On Tue, 5 Jun 2007 00:33:34 +0100, in comp.lang.c , "Malcolm McLean"
<regniz...@btinternet.comwrote:
>"Old Wolf" <oldw...@inspire.net.nzwrote in message
The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays.
>But multi-dimensional arrays in C are just hopeless.
Eh? They've worked fine for me for 20 years. Elucidate.

The problem is that you very rarely know the dimensions of a 2d array at
compile time, or if you do hardcoding it introduces an artificial fixed
limit to the program.
With 1d arrays it is not a problem since you can simply call malloc() or, in
certain cases, declare a "big enough" array. That's a lot harder with 2D
arrays.
Huh? You just need to call malloc once to allocate a block of pointers
to rows, then use malloc in an inner loop to assign each row pointer
to a block of size #cols. What's so hard about that?
Then finally the syntax for passing a 2D array to a function is too complex.
func(int **);

int **array;
/* ... setup ... */
func(array);

Complex? It's completely natural.
--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

Jun 5 '07 #92
On 2007-06-04 17:00:59 -0700, "Malcolm McLean" <re*******@btinternet.comsaid:
>
"Mark McIntyre" <ma**********@spamcop.netwrote in message
news:pk********************************@4ax.com...
>On Tue, 5 Jun 2007 00:33:34 +0100, in comp.lang.c , "Malcolm McLean"
<re*******@btinternet.comwrote:
>>>
"Old Wolf" <ol*****@inspire.net.nzwrote in message
The students who think arrays are pointers will
quickly become confused when working with
multi-dimensional arrays.

But multi-dimensional arrays in C are just hopeless.

Eh? They've worked fine for me for 20 years. Elucidate.
The problem is that you very rarely know the dimensions of a 2d array
at compile time, or if you do hardcoding it introduces an artificial
fixed limit to the program.
With 1d arrays it is not a problem since you can simply call malloc()
or, in certain cases, declare a "big enough" array. That's a lot harder
with 2D arrays.
Not really, to dynamically allocate a 2D array is essentially the same
process as allocating a 1D array, just squared :).

They're certainly easier to allocate in C++, but that doesn't mean
they're "hopeless" in C.
Then finally the syntax for passing a 2D array to a function is too complex.
It's not complex, it's non-existent. Functions cannot accept arrays as
parameters.
--
Clark S. Cox III
cl*******@gmail.com

Jun 5 '07 #93
On Jun 5, 12:11 pm, Francine.Ne...@googlemail.com wrote:
On Jun 5, 1:00 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
>>But multi-dimensional arrays in C are just hopeless.
The problem is that you very rarely know the dimensions of a 2d array at
compile time, or if you do hardcoding it introduces an artificial fixed
limit to the program.
You don't need to know the dimensions at compile
time; this restriction was removed from the
language 8 years ago (or is it 9?)

#include <stdio.h>

void foo(char const *name, int m, int n, int array[m][n])
{
for (int i = 0; i != m; ++i)
for (int j = 0; j != n; ++j)
printf("%s[%d][%d] = %d\n", name, i, j, array[i][j]);
}

int main(void)
{
int x[2][3] = { {1,2,3}, {4,5,6} };
int y[4][1] = { {10}, {11}, {12}, {13} };

foo("x", 2, 3, x);
foo("y", 4, 1, y);
}
With 1d arrays it is not a problem since you can simply call malloc() or, in
certain cases, declare a "big enough" array. That's a lot harder with 2D
arrays.

Huh? You just need to call malloc once to allocate a block of pointers
to rows, then use malloc in an inner loop to assign each row pointer
to a block of size #cols. What's so hard about that?
Then you don't have an array of arrays. You have an
array of pointers to arrays. Very different, even
though the same syntax can be used for member access.
Then finally the syntax for passing a 2D array to a function is too complex.

func(int **);
That is the syntax for passing a pointer to a pointer,
to a function. Not for passing arrays of arrays (which
is what we mean by "2D array").

Jun 5 '07 #94
On Mon, 4 Jun 2007 18:54:57 -0700, Clark Cox <cl*******@gmail.com>
wrote:
>On 2007-06-04 17:00:59 -0700, "Malcolm McLean" <re*******@btinternet.comsaid:
snip
>Then finally the syntax for passing a 2D array to a function is too complex.

It's not complex, it's non-existent. Functions cannot accept arrays as
parameters.
Of course they can. Or are you referring to the fact that the
argument evaluates to a pointer so the function never really sees it
as an array (but nevertheless has no trouble treating it as an array)?

And the syntax is only two characters more complex than passing a 1D
array.

void func(int[][N], int);
int array[M][N] = {...};
func(array, M);
Remove del for email
Jun 5 '07 #95
Old Wolf said:
On Jun 5, 12:11 pm, Francine.Ne...@googlemail.com wrote:
>On Jun 5, 1:00 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
>>But multi-dimensional arrays in C are just hopeless.
The problem is that you very rarely know the dimensions of a 2d
array at compile time, or if you do hardcoding it introduces an
artificial fixed limit to the program.

You don't need to know the dimensions at compile
time; this restriction was removed from the
language 8 years ago (or is it 9?)
The restriction was removed from the de jure language specification, not
from the de facto body of language implementations actually used in the
field. It is still not possible to rely on the VLA feature in code that
is intended to be maximally (or even 'fairly') portable.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 5 '07 #96
On Jun 5, 3:26 am, Old Wolf <oldw...@inspire.net.nzwrote:
On Jun 5, 12:11 pm, Francine.Ne...@googlemail.com wrote:
On Jun 5, 1:00 am, "Malcolm McLean" <regniz...@btinternet.comwrote:
With 1d arrays it is not a problem since you can simply call malloc() or, in
certain cases, declare a "big enough" array. That's a lot harder with 2D
arrays.
Huh? You just need to call malloc once to allocate a block of pointers
to rows, then use malloc in an inner loop to assign each row pointer
to a block of size #cols. What's so hard about that?

Then you don't have an array of arrays. You have an
array of pointers to arrays. Very different, even
though the same syntax can be used for member access.
I think that thinking of a block returned by malloc as a dynamically-
allocated array, even while not literally true since we all know that
array is a precisely-defined concept in C, is absolutely the right
thing to do. Pushing this one step further, identifying an ordered
collection of m vectors of length n with an m x n matrix (i.e. 2-
dimensional array) is equally sound conceptually.

Jun 5 '07 #97
On 4 Jun, 19:02, Johan Bengtsson <qwerty...@hotmail.comwrote:
Mark McIntyre wrote:
Since when did "asking for help" include correcting the advice you
were given? Do you normally correct your teacher?

Perhaps not normally, but sometimes the teacher is wrong (not implying
that it was the case here, just as a general statement). I have on
occasion when I was at school corrected my teacher simply because he was
wrong. It does happen.
I pointed out at 11
-that the moon was not a planet
-that Australia is not on the opposite side of the earth from Britain
(I considered New Zealand to be a better answer)

at 13
- I disputed that Germany did not invade Switzerland "because it was
neutral" (I'd seen their tank traps and thought that might have had
an
influence)

it may have helped that my mother was a primary school teacher.

I've corrected people running courses when they were in error.
--
Nick Keighley

Jun 5 '07 #98
Nick Keighley said:

<snip>
>
I've corrected people running courses when they were in error.

I once pointed out an error in the aptitude test I was taking. They
didn't believe me, of course - until I got the top score in the test
and turned out to be one of the top two students on the course[1]. Then
they checked it out (and of course vindicated my claim).

[1] Hi, Stuart. Remember? Q: "You already have 98.5 on this assignment
and 693/700 so far this unit. Why are you arguing so vehemently for
another half-a-blasted-mark?" A: "Because Stuart's on 692/700, that's
why..."

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 5 '07 #99
On 5 Jun, 00:06, Keith Thompson <k...@mib.orgwrote:
rich...@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <5cg85sF308o0...@mid.individual.net>,
Martin Ambuhl <mamb...@earthlink.netwrote:
>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.
I think you overstate the case. It is not literally true that an array
is the same as a constant pointer,

A synonym for "not literally true" is "not true".
but the statement does capture the
most distinctive aspect of C's array mechanism as compared with that of
other programming languages.

I strongly disagree. The distinctive aspect of C's array mechanism is
that *operations* on arrays are defined in terms of operations on
pointers.

Arrays are not pointers. Pointers are not arrays. Students who fail
to understand this are likely to make mistakes like:

void foo(int array[])
{
... sizeof array ...
/* expected to be the size of an array object, but it's really
the size of a pointer object */
}
A recent example. This was proposed (simplified from actual code)

typedef struct
{
size_t size;
unsigned char data[0];
} Msg_t;

I pointed out this was undefined by the standard and I provided
an example of a compiler that issued a warning of the use of an
extension. I was also curious as what they expected sizeof(Msg_t)
to return.

The proposed fix was:-

typedef struct
{
size_t size;
unsigned char *data;
} Msg_t;

--
Nick Keighley

Jun 5 '07 #100

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joseph Suprenant | last post: by
4 posts views Thread by Eric | last post: by
2 posts views Thread by Richard L Rosenheim | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.