Why do so few people know the difference between arrays and pointers.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Me

    Why do so few people know the difference between arrays and pointers.


    Just a question/observation out of frustration.

    I read in depth the book by Peter Van Der Linden entitled
    "Expert C Programming" (Deep C Secrets). In particular the
    chapters entitled:
    4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
    9: More about Arrays
    10: More about Pointers

    What blows me out of the water is the fact that 'every' programmer
    comming out of college that I've interviewed thinks that pointers
    and arrays are the same thing.

    They go so far as to tell me that, in the following code, 'arr' is
    a pointer to an array of integers. Or they tell me that 'arr' is a
    pointer to an 'int'. When this is not the case at all.

    int arr[100];

    The variable 'arr' IS and array of 100 integers...THAT S ITS TYPE MAN.
    Just like the TYPE of 'i' in the following is 'int'.

    int i;

    and the type of 'ch' in the following is 'char *'.

    char *ch = "string";

    The TYPE of 'arr' above is 'an array of 100 integers'. That's WHY you
    have to declare a pointer to it as follows:

    int (*p)[100];

    p = &arr; // Valid
    p = arr; // Invalid (compiler will warn you) but works because the
    address happens to be the same.

    Note: When you use an array in an expression or as an R-Value, the result
    of the
    operation yields a pointer to the first element in the array. Thus:

    int *ip = arr; // Valid: arr turns into an int pointer (a pointer to the
    first element in the array)
    // Not because ip is an 'int *' be because the array 'arr' is being
    used in an expression
    // as an R-Value.

    Man the C teachers in college aren't doing their job!



    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
  • Malcolm

    #2
    Re: Why do so few people know the difference between arrays and pointers.

    "Me" <bogus@bogus.co m> wrote in message[color=blue]
    >
    > " The Shocking Truth: C Arrays and Pointers Are NOT the
    > Same!
    >[/color]
    Array labels decay to pointers, so

    void foo(int *ptr);

    void bar(void)
    {
    int arr[100];

    foo(arr);
    }

    calls foo with a pointer to an array of integers.

    This ia all you need to know about C array pointer equivalence.

    If you start using multi-dimensional arrays and fancy declarations you
    deserve all that is coming to you.


    Comment

    • Me

      #3
      Re: Why do so few people know the difference between arrays and pointers.



      On Tue, 8 Jun 2004 22:30:49 +0100, Malcolm
      <malcolm@55bank .freeserve.co.u k> wrote:
      [color=blue]
      > "Me" <bogus@bogus.co m> wrote in message[color=green]
      >>
      >> " The Shocking Truth: C Arrays and Pointers Are NOT the
      >> Same!
      >>[/color]
      > Array labels decay to pointers, so
      >
      > void foo(int *ptr);
      >
      > void bar(void)
      > {
      > int arr[100];
      >
      > foo(arr);
      > }
      >
      > calls foo with a pointer to an array of integers.[/color]

      Actually you are mistaken...prob ably you just mis-typed.

      It calls foo with a pointer to an int.... not a pointer to an array of
      ints.
      It just so happens that what the pointer is pointing to is the first int in
      the allocated array of 100 ints, but 'foo' doesn't know that. foo() just
      thinks
      it is an 'int' pointer.
      [color=blue]
      >
      > This ia all you need to know about C array pointer equivalence.
      >
      > If you start using multi-dimensional arrays and fancy declarations you
      > deserve all that is coming to you.
      >
      >[/color]



      --
      Using Opera's revolutionary e-mail client: http://www.opera.com/m2/

      Comment

      • E. Robert Tisdale

        #4
        Troll Alert: Why do so few people know the difference between arraysand pointers.

        Something that calls itself Me wrote:
        [color=blue]
        > Just a question/observation out of frustration.
        >
        > I read in depth the book by Peter Van Der Linden entitled
        > "Expert C Programming" (Deep C Secrets).
        > In particular the chapters entitled:
        > 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
        > 9: More about Arrays
        > 10: More about Pointers[/color]

        Obvious hyperbole.
        [color=blue]
        > What blows me out of the water is the fact that
        > 'every' programmer comming out of college that I've interviewed
        > thinks that pointers and arrays are the same thing.
        >
        > They go so far as to tell me that, in the following code,
        > 'arr' is a pointer to an array of integers.
        > Or they tell me that 'arr' is a pointer to an 'int'.
        > When this is not the case at all.
        >
        > int arr[100];
        >
        > The variable 'arr' IS and array of 100 integers...THAT S ITS TYPE MAN.
        > Just like the TYPE of 'i' in the following is 'int'.
        >
        > int i;
        >
        > and the type of 'ch' in the following is 'char*'.
        >
        > char* ch = "string";
        >
        > The TYPE of 'arr' above is 'an array of 100 integers'.
        > That's WHY you have to declare a pointer to it as follows:
        >
        > int (*p)[100];
        >
        > p = &arr; // Valid
        > p = arr; // Invalid (compiler will warn you)
        > // but works because the address happens to be the same.
        >
        > Note: When you use an array in an expression or as an R-Value,
        > the result of the operation
        > yields a pointer to the first element in the array. Thus:
        >
        > int* ip = arr; // Valid: arr turns into an int pointer
        > // (a pointer to the first element in the array)
        > // Not because ip is an 'int *'
        > // but because the array 'arr' is being used
        > // in an expression as an R-Value.
        >
        > Man the C teachers in college aren't doing their job![/color]

        Do you feel better now that you got that off your chest?

        Comment

        • E. Robert Tisdale

          #5
          Re: Why do so few people know the difference between arrays and pointers.

          Me wrote:[color=blue]
          >
          >
          > Malcolm wrote:
          >[color=green]
          >> Array labels decay to pointers, so
          >>
          >> void foo(int *ptr);
          >>
          >> void bar(void) {
          >> int arr[100];
          >> foo(arr);
          >> }
          >>
          >> calls foo with a pointer to an array of integers.[/color]
          >
          > Actually you are mistaken...prob ably you just mis-typed.
          >
          > It calls foo with a pointer to an int....
          > not a pointer to an array of ints.[/color]

          Correct.
          [color=blue]
          > It just so happens that what the pointer is pointing to
          > is the first int in the allocated array of 100 ints,[/color]

          I'm pretty sure that you don't mean to imply that
          it is just an accident that arr is converted
          into a pointer to the first element of arr.
          [color=blue]
          > but 'foo' doesn't know that.
          > foo() thinks [that] it is just an 'int' pointer.[/color]

          Comment

          • CBFalconer

            #6
            Re: Why do so few people know the difference between arrays andpointers.

            Me wrote:[color=blue]
            >[/color]
            .... snip ...[color=blue]
            >
            > What blows me out of the water is the fact that 'every' programmer
            > comming out of college that I've interviewed thinks that pointers
            > and arrays are the same thing.[/color]

            When I got out of college in nineteen-<mumble> I certainly didn't
            think so. :-)

            --
            A: Because it fouls the order in which people normally read text.
            Q: Why is top-posting such a bad thing?
            A: Top-posting.
            Q: What is the most annoying thing on usenet and in e-mail?


            Comment

            • Kieran Simkin

              #7
              Re: Why do so few people know the difference between arrays and pointers.

              "Me" <bogus@bogus.co m> wrote in message
              news:opr9anp8hu abpysj@danocdhc p011136.america s.nokia.com...[color=blue]
              >
              > Just a question/observation out of frustration.
              >
              > I read in depth the book by Peter Van Der Linden entitled
              > "Expert C Programming" (Deep C Secrets). In particular the
              > chapters entitled:
              > 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
              > 9: More about Arrays
              > 10: More about Pointers
              >
              > What blows me out of the water is the fact that 'every' programmer
              > comming out of college that I've interviewed thinks that pointers
              > and arrays are the same thing.
              >
              > They go so far as to tell me that, in the following code, 'arr' is
              > a pointer to an array of integers. Or they tell me that 'arr' is a
              > pointer to an 'int'. When this is not the case at all.
              >
              > int arr[100];
              >
              > The variable 'arr' IS and array of 100 integers...THAT S ITS TYPE MAN.
              > Just like the TYPE of 'i' in the following is 'int'.
              >
              > int i;
              >
              > and the type of 'ch' in the following is 'char *'.
              >
              > char *ch = "string";
              >
              > The TYPE of 'arr' above is 'an array of 100 integers'. That's WHY you
              > have to declare a pointer to it as follows:
              >
              > int (*p)[100];
              >
              > p = &arr; // Valid
              > p = arr; // Invalid (compiler will warn you) but works because the
              > address happens to be the same.
              >
              > Note: When you use an array in an expression or as an R-Value, the result
              > of the
              > operation yields a pointer to the first element in the array. Thus:
              >
              > int *ip = arr; // Valid: arr turns into an int pointer (a pointer to the
              > first element in the array)
              > // Not because ip is an 'int *' be because the array 'arr' is being
              > used in an expression
              > // as an R-Value.
              >
              > Man the C teachers in college aren't doing their job![/color]

              I'm currently teaching my self C from books and the internet and when I
              first started learning, constant mentions of the "equivalenc e" of arrays and
              pointers confused the hell out of me because based on what I understood
              (correctly) about pointers and arrays, there is nothing equivalent about
              them, they're completely different although related things. What would have
              saved me a bunch of headscratching would be definitions of an array and a
              pointer and then statements of the following facts:

              An array, when referenced without an element number decays into a pointer to
              that array's first element (without needing to use the & operator)

              #include <stdio.h>
              int main (void) {
              int foo[10];
              int *bar;
              bar = foo;
              printf("%i",bar[1]); /* is equivalent to the following */
              printf("%i",*(b ar+1)); /* both will output the contents of foo[1] */
              return 0;
              }

              Based on a understanding of pointers and arrays, this explains all that
              needs to be explained without using the words "arrays and pointers are
              equivalent" or similar which I've seen in several places and are just
              blatantly wrong.

              Excuse me and please do correct me if I've used any incorrect terminology
              here, as I said, I'm still learning.
              [color=blue]
              >
              >
              >
              > --
              > Using Opera's revolutionary e-mail client: http://www.opera.com/m2/[/color]


              Comment

              • Daniel Rudy

                #8
                Re: Why do so few people know the difference between arrays and pointers.

                And somewhere around the time of 06/08/2004 14:10, the world stopped and
                listened as Me contributed the following to humanity:
                [color=blue]
                > Just a question/observation out of frustration.
                >
                > I read in depth the book by Peter Van Der Linden entitled
                > "Expert C Programming" (Deep C Secrets). In particular the
                > chapters entitled:
                > 4: The Shocking Truth: C Arrays and Pointers Are NOT the Same!
                > 9: More about Arrays
                > 10: More about Pointers
                >
                > What blows me out of the water is the fact that 'every' programmer
                > comming out of college that I've interviewed thinks that pointers
                > and arrays are the same thing.
                >[/color]

                Yes and no. An array is a list of data of some type. A pointer is a
                reference that points to some data in memory.

                Internally, the array identifier acting as the base address, coupled
                with the index * data size, generates a pointer to that data element in
                memory. All of this is internal to the compiler/linker and is usually
                transparent to the programmer, at least that's what my expeiriance has been.

                --
                Daniel Rudy

                Email address has been encoded to reduce spam.
                Remove all numbers, then remove invalid, email, no, and spam to reply.

                Comment

                • Mike Wahler

                  #9
                  Re: Why do so few people know the difference between arrays and pointers.


                  "Malcolm" <malcolm@55bank .freeserve.co.u k> wrote in message
                  news:ca5b0d$tse $1@news7.svr.po l.co.uk...[color=blue]
                  > "Me" <bogus@bogus.co m> wrote in message[color=green]
                  > >
                  > > " The Shocking Truth: C Arrays and Pointers Are NOT the
                  > > Same!
                  > >[/color]
                  > Array labels decay to pointers, so
                  >
                  > void foo(int *ptr);
                  >
                  > void bar(void)
                  > {
                  > int arr[100];
                  >
                  > foo(arr);
                  > }
                  >
                  > calls foo with a pointer to an array of integers.[/color]

                  Wrong. Calls 'foo()' with a pointer to an 'int'.
                  (type 'int*'). Pointer-to-int and pointer-to-array-of-ints
                  are not the same type.

                  int *p; /* pointer to int */
                  int (*pa)[100]; /* pointer to array of 100 ints */

                  [color=blue]
                  > This ia all you need to know about C array pointer equivalence.[/color]

                  I don't think one should 'know' incorrect information.[color=blue]
                  >
                  > If you start using multi-dimensional arrays and fancy declarations you
                  > deserve all that is coming to you.[/color]

                  What's 'wrong' with using multi-dimensional arrays,
                  and what will come to me if I use one?

                  I won't ask what you consider a 'fancy' declaration.

                  -Mike


                  Comment

                  • *a

                    #10
                    Re: Why do so few people know the difference between arrays and pointers.

                    hi all; i'm a self-taught C-hobbyst; i want to sing k&r's praises: their
                    book was somewhat hard for me, i proceeded slowly, sometimes at the "speed"
                    of few pages/week, but it's impossible to confuse pointer with arrays then;
                    the problem is C needs a non-superficial study


                    Comment

                    • Keith Thompson

                      #11
                      Re: Why do so few people know the difference between arrays and pointers.

                      "Kieran Simkin" <kieran@digit al-crocus.com> writes:
                      [...][color=blue]
                      > An array, when referenced without an element number decays into a pointer to
                      > that array's first element (without needing to use the & operator)[/color]

                      An array name (an identifer that refers to an array object) decays
                      into a pointer to the first element whenever it appears in an
                      expression, unless it's the operand of a sizeof or unary "&" operator.

                      Note that the decay even occurs in an indexing expression:

                      int array_obj[10];
                      int x;
                      ...
                      x = array_obj[5];

                      In the assignment, the name "array_obj" decays to a pointer to the
                      first element of the array object. The indexing operator takes two
                      operands, a pointer and an integer.

                      Read section 6 of the C FAQ.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@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.

                      Comment

                      • Michael Mair

                        #12
                        Re: Why do so few people know the difference between arrays and pointers.

                        Cheerio,

                        [color=blue]
                        > What blows me out of the water is the fact that 'every' programmer
                        > comming out of college that I've interviewed thinks that pointers
                        > and arrays are the same thing.[/color]

                        If it helps you feel better: We have a finite element code we are
                        working with, written in C, largely undocumented, many thousand lines,
                        and are looking for students to work for us.
                        We get many applications from so-called software engineering students
                        in their final year. Every single interview came to the point that
                        they did not "know" any programming language at all while boasting
                        knowledge of a multitude. We never ever did even get to the point
                        where such "fine details" like the differences between arrays and
                        pointers would have played a role. Same thing when we were looking
                        for someone doing some things in Matlab for us.
                        So, do not tell me about frustration ;-)

                        [color=blue]
                        > Man the C teachers in college aren't doing their job![/color]

                        Based on my sad experiences, I am at the moment teaching C to
                        beginners in order to get someone able to work with us.
                        I did never tell anyone that pointers and arrays were equivalent,
                        in fact, I only started with "&array[0]" and came up with the "idea"
                        of using "array" later on, explained exactly what pointers are,
                        what arrays are, how they relate -- nonetheless, "pointer equals array"
                        is exactly the sort of idea I see them using in their assignments.
                        I try to exorcise it in the next lesson but somehow it sticks. I
                        suggested a couple of C books to the students but somehow I never
                        caught anyone reading one of them. They just get the cheapest C books
                        off Amazon thinking this will do... :-/
                        Seeing my efforts having no effect for at least ten percent of my
                        students, I can only say: Maybe, the fault is not only in the teachers.

                        Another thing is that you have to learn some things entirely by
                        yourself. Only after you made a serious mistake or saw the outcome
                        of some mistake you will remember it by heart. A friend of mine
                        for example needed to find out by himself that a quadratic time
                        complexity is _much_ worse than linear time complexity of an
                        algorithm - even though he theoretically knew it beforehand.
                        Looking at myself and at my colleagues as well, I'd say that every
                        year I am still learning quite a lot of things in all the programming
                        languages I am using.



                        Just my two cents
                        Michael

                        Comment

                        • Vijay Kumar R Zanvar

                          #13
                          Re: Why do so few people know the difference between arrays and pointers.


                          "Keith Thompson" <kst-u@mib.org> wrote in message news:lnvfi1maew .fsf@nuthaus.mi b.org...[color=blue]
                          > "Kieran Simkin" <kieran@digit al-crocus.com> writes:
                          > [...][color=green]
                          > > An array, when referenced without an element number decays into a pointer to
                          > > that array's first element (without needing to use the & operator)[/color]
                          >
                          > An array name (an identifer that refers to an array object) decays
                          > into a pointer to the first element whenever it appears in an
                          > expression, unless it's the operand of a sizeof or unary "&" operator.
                          >[/color]

                          .... and when a character string literal is used to initialize an array.

                          [color=blue]
                          > Note that the decay even occurs in an indexing expression:
                          >
                          > int array_obj[10];
                          > int x;
                          > ...
                          > x = array_obj[5];
                          >
                          > In the assignment, the name "array_obj" decays to a pointer to the
                          > first element of the array object. The indexing operator takes two
                          > operands, a pointer and an integer.
                          >
                          > Read section 6 of the C FAQ.
                          >
                          > --
                          > Keith Thompson (The_Other_Keit h) kst-u@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.[/color]


                          Comment

                          • Christian Bau

                            #14
                            Re: Why do so few people know the difference between arrays and pointers.

                            In article <ca6h4p$elo$1@i nfosun2.rus.uni-stuttgart.de>,
                            Michael Mair <mairRemove_for _mailinG@ians.u ni-stuttgart.de> wrote:
                            [color=blue]
                            > Another thing is that you have to learn some things entirely by
                            > yourself. Only after you made a serious mistake or saw the outcome
                            > of some mistake you will remember it by heart. A friend of mine
                            > for example needed to find out by himself that a quadratic time
                            > complexity is _much_ worse than linear time complexity of an
                            > algorithm - even though he theoretically knew it beforehand.
                            > Looking at myself and at my colleagues as well, I'd say that every
                            > year I am still learning quite a lot of things in all the programming
                            > languages I am using.[/color]

                            In my experience, different people learn in different ways. Some prefer
                            to actually try out things because it helps them learning. However,
                            trying out things is also useful to check if the theory is really
                            correct (I once saw a C++ programmer using the quicksort algorithm, but
                            for sorting an array of variable sized items, and the underlying methods
                            for changing an item took time O (total size of all items) when changing
                            the size of an item. The code was not O (N log N), and it was not fast
                            enough (noticable delay in the user interface in non-trivial cases)).

                            Also, an algorithm with higher time complexity may be faster than one of
                            lower complexity up to a certain size. Consider method A taking (N^(2/3)
                            log^2 (N)) nanoseconds, and method B taking N^0.7 nanoseconds. For large
                            N, method A is faster. For any problem that can be solved in a billion
                            years, method A is slower.

                            Comment

                            • Giorgos Keramidas

                              #15
                              Re: Why do so few people know the difference between arrays and pointers.

                              Michael Mair <mairRemove_for _mailinG@ians.u ni-stuttgart.de> writes:[color=blue][color=green]
                              > > Man the C teachers in college aren't doing their job![/color]
                              >
                              > Based on my sad experiences, I am at the moment teaching C [...]
                              > I suggested a couple of C books to the students but somehow I never
                              > caught anyone reading one of them. They just get the cheapest C books
                              > off Amazon thinking this will do... :-/ Seeing my efforts having no
                              > effect for at least ten percent of my students, I can only say: Maybe,
                              > the fault is not only in the teachers.[/color]

                              I don't think it is only the teachers' fault.

                              While being a student at the Computer Engineering & Informatics
                              Department here in Patras, Greece, I used to say that about 80% of the
                              students who enter my department every year are not interested (and,
                              quite probably, they never will be) in learning anything beyond the
                              absolutely necessary stuff they have to "put up with" in order to get a
                              degree. Only a small number of the remaining 20% has the patience and
                              perseverance it takes to learn more about the fine details of
                              programming--regardless of the language in question.
                              [color=blue]
                              > Another thing is that you have to learn some things entirely by
                              > yourself.[/color]

                              So very true.

                              - Giorgos

                              Comment

                              Working...