Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

Your view of FAQ section.


One of the most repeatedly given pointers is to

http://c-faq.com/aryptr/aryptr2.html

which discusses pointers and arrays and the subtle differences between

        char a[] = "hello";
        char *p = "world";

Now, my question is this - how do you feel about the way the FAQ
describes the compiler working. We already now that pointers and arrays
may or may not compile differently and may or may not be more efficient
than each other depending on target HW.

But should the FAQ really go into detail about how the compiler
calculates element addresses?

e.g

,----
| It is useful to realize that a reference like x[3] generates different code depending on whether x is an array or a pointer. Given the declarations above, when the compiler
| sees the expression a[3], it emits code to start at the location ``a'', move three past it, and fetch the character there. When it sees the expression p[3], it emits code to
| start at the location ``p'', fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to. In other words, a[3] is three places past
| (the start of) the object named a, while p[3] is three places past the object pointed to by p. In the example above, both a[3] and p[3] happen to be the character 'l', but
| the compiler gets there differently. (The essential difference is that the values of an array like a and a pointer like p are computed differently whenever they appear in
| expressions, whether or not they are being subscripted, as explained further in question 6.3.) See also question 1.32.
`----

Yes.

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Can you explain why?

Does the standard stipulate how the compiler must generate the addresses?

Ok, that was overly terse.  But the FAQ doesn't really talk about
generated code; it's more of a description of what happens in the
abstract machine, which is important to an understanding of what the
code actually means.

Do you have a better way to describe the difference between a[3] and
p[3] (where a is an array object and p is a pointer object)?

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

"Keith Thompson" <k@mib.org> wrote in message

news:lntztpyfc2.fsf@nuthaus.mib.org...

Exactly. People are looking for an explanation, not a definition. Sometimes
it is better to say something which is slightly inaccurate, or only
represents the general case, but is concrete, rather than give the latter of
the law.

 Array - pointer equivalance is a classic case. Unless you go through a
stage of thinking that arrays and pointers are equivalent, you will never
understand why they are not equivalent.

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

No. But telling us how the compiler may or may not do it is overly
presumptuous IMO.

Only a while ago I was sure (as stated in K&R2 too btw) that using
pointers and not arrays generated faster code as a general rule. Some
work I have done since has pretty much made me change my mind.

Richard wrote:
> Keith Thompson <k@mib.org> writes:

>> Do you have a better way to describe the difference between a[3] and
>> p[3] (where a is an array object and p is a pointer object)?

> No. But telling us how the compiler may or may not do it is overly
> presumptuous IMO.

     The goal of the FAQ is explanation, not specification.
As such, it quite frequently ventures outside the territory
marked off by the Standard -- for example, it talks not only
about "undefined behavior" but also about "segmentation
violation" and "general protection fault."  I see nothing
amiss with the FAQ speaking about generated code (which itself
is beyond the scope of the Standard) to highlight a point and
bring the reader closer to the "Aha!" moment.

--
Eric Sosman
esos@acm-dot-org.invalid

Malcolm McLean wrote, On 03/06/07 10:17:

> "Keith Thompson" <k@mib.org> wrote in message

<snip>

>> Do you have a better way to describe the difference between a[3] and
>> p[3] (where a is an array object and p is a pointer object)?

> Exactly. People are looking for an explanation, not a definition.
> Sometimes it is better to say something which is slightly inaccurate, or
> only represents the general case, but is concrete, rather than give the
> latter of the law.

To an extent, yes.

> Array - pointer equivalance is a classic case. Unless you go through a
> stage of thinking that arrays and pointers are equivalent, you will
> never understand why they are not equivalent.

However here I completely disagree.

I can't remember ever thinking that arrays and pointers were equivalent,
and I can see no reason why someone would need to believe that
particular fallacy before understanding why it is false. Explain one
concept in one lesson, make sure it is understood, explain the other
concept in another lesson and make sure it is understood, and then
explain that because of the way C implements arrays you don't pass
arrays and when you can treat one as the other.
--
Flash Gordon

"Malcolm McLean" <regniz@btinternet.com> writes:

[...]

> Array - pointer equivalance is a classic case. Unless you go through a
> stage of thinking that arrays and pointers are equivalent, you will
> never understand why they are not equivalent.

Either I don't understand that statement, or I completely disagree
with it; I'm not sure which.

Surely it's possible for someone learning C to understand from the
beginning that arrays and pointers are very different things, if the
concepts are just explained correctly on the first exposure.

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Malcolm McLean wrote:
>  Array - pointer equivalance is a classic case. Unless you go through a
> stage of thinking that arrays and pointers are equivalent, you will never
> understand why they are not equivalent.

I don't think I ever went through the stage of believing that they
were equivalent, but I claim I understand why they are not.

Of course, C wasn't my first language -- it wasn't even my fourth --
and I hacked BCPL before I ever met C in real life.

--
"Anything can happen in the next half-hour."                         /Stingray/

Hewlett-Packard Limited registered office:                Cain Road, Bracknell,
registered no: 690597 England                                    Berks RG12 1HN

"Keith Thompson" <k@mib.org> wrote in message

news:lnvee4wwy3.fsf@nuthaus.mib.org...
> "Malcolm McLean" <regniz@btinternet.com> writes:
> [...]
>> Array - pointer equivalance is a classic case. Unless you go through a
>> stage of thinking that arrays and pointers are equivalent, you will
>> never understand why they are not equivalent.

> Either I don't understand that statement, or I completely disagree
> with it; I'm not sure which.

> Surely it's possible for someone learning C to understand from the
> beginning that arrays and pointers are very different things, if the
> concepts are just explained correctly on the first exposure.

You've got an unusual mind. Which is probably a strength, as a programmer.
However most people think and learn in far less abstract way.

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

I still disagree.  I think most people are perfectly capable of
understanding C pointers and arrays without *ever* having the delusion
that they're the same thing.  And anyone who's not capable of that
likely will never really understand either arrays or pointers.

(I'm not saying that anyone who has thought arrays and pointers are
the same thing can never understand them.  The misconception is
unnecessary, not fatal.)

--
Keith Thompson (The_Other_Keith) k@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson wrote, On 07/06/07 08:02:

I agree. I've managed to explain the concepts of arrays and pointer to
people who have NO programming experience. It is really very easy to
explain in a classroom.

Desks are arranged in a traditional way in the classroom. OK, here is a
2 dimensional array of desks. We will number the rows from 0, and the
columns from 0, so John is in desk[3][5].

Now for pointers. My finger is pointing to desk[3][5], now I'm going to
increment it, it is now pointing at desk[3][6] where Jenny is.

Simple. Then, once they understand the general concepts of arrays and
pointers you can go on to explain the wrinkles of array in C.

As I say, I know that this type of explanation works (possibly with a
bit more discussion) since I've used it with non-computer people and
managed to go on to explain why things are not working in buggy programs.

> (I'm not saying that anyone who has thought arrays and pointers are
> the same thing can never understand them.  The misconception is
> unnecessary, not fatal.)

Agreed.
--
Flash Gordon
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc