Re: K&R2, exercise 6.4

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

    Re: K&R2, exercise 6.4

    arnuld said:
    I get a compile time error. I know this has something to do with arrays
    and pointers but I can't figure out what is wrong with line 56:
    >
    >
    LINE 56: psnode++ = &root_node;
    What do you think that line does? For my own part, I can make no sense
    whatsoever out of it.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
  • arnuld

    #2
    Re: K&amp;R2, exercise 6.4

    On Thu, 01 May 2008 10:56:26 +0000, Richard Heathfield wrote:
    >arnuld said:
    >LINE 56: psnode++ = &root_node;
    What do you think that line does? For my own part, I can make no sense
    whatsoever out of it.
    some earlier lines are:

    struct snode *arr_psnodes[ARRSIZE];
    struct snode **psnode;

    psnode = arr_psnodes;
    psnode++ = &root_node;


    -- arr_psnodes is an array of pointers to structures
    -- psnode is a pointer to arr_psnodes ( to its first element in reality )
    -- psnodes++ will point to 1st element of arr_psnodes and then so on.

    therefore,

    psnodes++ = &root_node;

    means, the arr_snodes's 1st element points to root_node (the first node)
    and ++ will make next node to get pointed by 2nd element of arr_psnodes.

    just like array indexing:

    int i = 0;

    arr_psnodes[0] = root_node
    arr_psnodes[1] = root_node (next one in linked-list)


    psnodes ++ = &root_node
    psnode++ = &root_node (next one in linked-list)



    since psnodes is a ** and root_node is a *, hence only a ** can take the
    address of a * which is quite logical I think



    --

    my email ID is @ the above address

    Comment

    • santosh

      #3
      OT - Re: K&amp;R2, exercise 6.4

      arnuld wrote:

      <snip>

      Are you aware that your address munging technique is faulty? The valid
      domain <nopain.comis likely being sent spam e-mail because of your
      use of that domain in your munged address in the Sender field of your
      posts' header. Read the following links (especially the first two) to
      learn how to properly use munged e-mail addresses.

      <http://www.2kevin.net/munging.html>
      <http://www.faqs.org/faqs/net-abuse-faq/munging-address/>
      <http://en.wikipedia.or g/wiki/Address_munging >
      <http://en.wikipedia.or g/wiki/Anti-spam_techniques _%28e-mail%29>
      <http://spam.abuse.net/>
      <http://www.spamhelp.or g/>

      Comment

      • Richard Heathfield

        #4
        Re: K&amp;R2, exercise 6.4

        arnuld said:
        >On Thu, 01 May 2008 10:56:26 +0000, Richard Heathfield wrote:
        >
        >>arnuld said:
        >
        >>LINE 56: psnode++ = &root_node;
        >
        >What do you think that line does? For my own part, I can make no sense
        >whatsoever out of it.
        >
        some earlier lines are:
        >
        struct snode *arr_psnodes[ARRSIZE];
        struct snode **psnode;
        >
        psnode = arr_psnodes;
        That doesn't actually matter. It's the line itself that makes no sense, not
        its predecessors.
        psnode++ = &root_node;
        >
        -- arr_psnodes is an array of pointers to structures
        -- psnode is a pointer to arr_psnodes ( to its first element in reality
        )
        -- psnodes++ will point to 1st element of arr_psnodes and then so on.
        psnodes++ is a pointer value, but not an lvalue. You can't assign to a
        value. Let's take a simpler example:

        int i = 6;
        i++ = 42;

        If this meant anything at all (which it doesn't), it would mean "change the
        value of i to 7 and, while you're at it, change the value of 6 to 42".
        (Note: NOT the value of i, but the value of 6.) This doesn't work. What
        could it mean, conceptually? If 6 now has the value 42, what is 6/7? Is it
        0? Or 6? Or even 42? All are defensible.
        therefore,
        >
        psnodes++ = &root_node;
        >
        means, the arr_snodes's 1st element points to root_node (the first node)
        and ++ will make next node to get pointed by 2nd element of arr_psnodes.
        No, ++ changes psnodes (which is of type struct snode **, so it points at a
        struct snode *) so that, instead of pointing to wherever it was pointing,
        it is pointing to the /next/ struct snode * along from wherever that was.
        In the process, it yields, as its value, the value that psnodes had at the
        previous sequence point. Assigning to this value makes no more sense than
        changing the value of 6.
        just like array indexing:
        >
        int i = 0;
        >
        arr_psnodes[0] = root_node
        arr_psnodes[1] = root_node (next one in linked-list)
        There is a huge difference between x[1] and x++, and it's a difference you
        should have learned a long time before Exercise 6. Return to Section 1.5.2
        of K&R2, and proceed more slowly from now on. Rushing won't get you
        anywhere.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • arnuld

          #5
          Re: K&amp;R2, exercise 6.4

          On May 1, 9:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
          >arnuld said:
          int i = 6;
          i++ = 42;
          >
          If this meant anything at all (which it doesn't), it would mean "change the
          value of i to 7 and, while you're at it, change the value of 6 to 42".
          (Note: NOT the value of i, but the value of 6.) This doesn't work. What
          could it mean, conceptually? If 6 now has the value 42, what is 6/7? Is it
          0? Or 6? Or even 42? All are defensible.

          those are values, I am talking about pointers to arrays:

          int arr[3];
          int* pi = arr;

          then ++pi will not point to next element of the array ?



          therefore,
          >
          psnodes++ = &root_node;
          and this is what I did. The only difference is 2 levels of
          indirection.

          Comment

          • Richard Heathfield

            #6
            Re: K&amp;R2, exercise 6.4

            arnuld said:
            >On May 1, 9:33 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
            >
            >>arnuld said:
            >
            >
            >int i = 6;
            >i++ = 42;
            >>
            >If this meant anything at all (which it doesn't), it would mean "change
            >the value of i to 7 and, while you're at it, change the value of 6 to
            >42". (Note: NOT the value of i, but the value of 6.) This doesn't work.
            >What could it mean, conceptually? If 6 now has the value 42, what is
            >6/7? Is it 0? Or 6? Or even 42? All are defensible.
            >
            >
            those are values, I am talking about pointers to arrays:
            Pointer values are still values.
            >
            int arr[3];
            int* pi = arr;
            >
            then ++pi will not point to next element of the array ?
            Yes, but the expression ++pi yields as its result a VALUE, not an object.
            You can't assign to values. The expression:

            ++pi = whatever;

            is illegal. I explained this in my last reply.
            therefore,
            >>
            psnodes++ = &root_node;
            >
            and this is what I did. The only difference is 2 levels of
            indirection.
            There is no difference. You are still trying to assign to a value, and that
            isn't legal in C.

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • arnuld

              #7
              Re: K&amp;R2, exercise 6.4

              On May 2, 12:39 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
              >arnuld said:
              int arr[3];
              int* pi = arr;
              then ++pi will not point to next element of the array ?
              Yes, but the expression ++pi yields as its result a VALUE, not an object.
              You can't assign to values. The expression:
              >
              ++pi = whatever;
              >
              is illegal. I explained this in my last reply.
              pretty much complicated concept. pi++ and ++pi both never yield a
              pointer as result and hence can not be used for assignment but arr[i+
              +] can be used. Pointers do not have much useful connection with
              arrays then .

              There is no difference. You are still trying to assign to a value, and that
              isn't legal in C.
              pi = &i; does not yield a value
              pi++ = &i; yields a value

              right ?



              I thought C was simpler than C++ but I think it is as complicated as C+
              +

              Comment

              • Richard Heathfield

                #8
                Re: K&amp;R2, exercise 6.4

                arnuld said:
                >On May 2, 12:39 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                >>arnuld said:
                >
                >
                int arr[3];
                int* pi = arr;
                >
                then ++pi will not point to next element of the array ?
                >
                >Yes, but the expression ++pi yields as its result a VALUE, not an
                >object. You can't assign to values. The expression:
                >>
                > ++pi = whatever;
                >>
                >is illegal. I explained this in my last reply.
                >
                pretty much complicated concept. pi++ and ++pi both never yield a
                pointer as result
                Yes, they do. If pi is a pointer, so is pi++, and so is ++pi. But they are
                pointer *values*, not pointer *objects*. You can't assign to values, only
                to objects.
                and hence can not be used for assignment but arr[i++] can be used.
                So can pi[i++]. So what?
                Pointers do not have much useful connection with arrays then .
                I don't see how you deduce this from the information provided.
                >There is no difference. You are still trying to assign to a value, and
                >that isn't legal in C.
                >
                pi = &i; does not yield a value
                Yes, it *does*. The expression pi = &i does yield a value. You can
                demonstrate this by writing pj = pi = &i; where pj is of the same type as
                pi.
                pi++ = &i; yields a value
                >
                right ?
                Right. *All* expressions yield values.
                I thought C was simpler than C++ but I think it is as complicated as C+
                +
                C++ is vastly more complicated. I'm reasonably sure that everything I've
                told you in this thread applies equally to C++.

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -http://www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • arnuld

                  #9
                  Re: K&amp;R2, exercise 6.4

                  On May 2, 12:57 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                  >arnuld said:
                  >pi++ and ++pi both never yield a pointer as result
                  Yes, they do. If pi is a pointer, so is pi++, and so is ++pi. But they are
                  pointer *values*, not pointer *objects*. You can't assign to values, only
                  to objects.

                  int i = 3;

                  i is variable and it has value 3

                  int *p = &i;

                  p is a pointer variable and its value is the memory address of i.

                  Right ?

                  (I never use word object for things stored in memory. That confuses me
                  with Classes and Objects. I simply call them variables or functions)

                  and hence can not be used for assignment but arr[i++] can be used.
                  >
                  So can pi[i++]. So what?

                  pi++ and pi[i++] are different ?



                  C++ is vastly more complicated. I'm reasonably sure that everything I've
                  told you in this thread applies equally to C++.

                  I am comparing the:

                  complexity of understanding arrays of pointers to structures/pointers and
                  pointers to pointers to arrays of pointers.

                  with

                  C++ Templates and other constructs like std::string and std::vector

                  you never do arrays in C unless you have memory-constraints like Embedded
                  Programming. Most of the time, in C++, we use strings and vectors and
                  Std. Lib. algorithms & as per my understanding, Templates are simpler
                  than handling Arrays and Pointers to Pointers to arrays of pointers to
                  something.


                  I am NOT arguing with you Richard. I know you are trying to help me and I
                  posted this question here because I *really* want to understand arrays
                  and pointers because the power of C++ Std. Lib. algorithms, strings and
                  Templates is, in all, originates from Arrays and Pointers. I want to
                  master C and contribute back to community by writing Hurd. I was just an
                  average student in the class, not with much IQ, hence I am takinig longer
                  time to understand these things:



                  ----

                  my email ID is @ the above address

                  Comment

                  • Richard Heathfield

                    #10
                    Re: K&amp;R2, exercise 6.4

                    arnuld said:
                    >On May 2, 12:57 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
                    >
                    >>arnuld said:
                    <snip>
                    and hence can not be used for assignment but arr[i++] can be used.
                    >>
                    >So can pi[i++]. So what?
                    >
                    >
                    pi++ and pi[i++] are different ?
                    Yes, very much so. pi++ changes the value stored in the pi object, and has
                    no effect whatsoever on i. pi[i++] is equivalent to *(pi + i++), which
                    modifies the i object (adding 1 to it) and which yields the prior value of
                    i for addition to pi, giving a pointer value which can be dereferenced to
                    access the object being pointed at.
                    >C++ is vastly more complicated. I'm reasonably sure that everything I've
                    >told you in this thread applies equally to C++.
                    >
                    >
                    I am comparing the complexity of understanding arrays of pointers to
                    structures/pointers and pointers to pointers to arrays of pointers
                    with C++ Templates and other constructs like std::string and std::vector
                    So in C++ you have all this array and pointer stuff to understand, AND all
                    that std::string and std::vector stuff to understand AS WELL. I don't see
                    how this makes C++ simpler.
                    you never do arrays in C unless you have memory-constraints
                    Don't I? I thought I used them quite a lot, actually.

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • Richard Tobin

                      #11
                      Re: K&amp;R2, exercise 6.4

                      In article <n-OdnX00htjwVYfVn Z2dnUVZ8tChnZ2d @bt.com>,
                      Richard Heathfield <rjh@see.sig.in validwrote:
                      >pi++ = &i; yields a value
                      >>
                      > right ?
                      >Right.
                      Er...

                      -- Richard
                      --
                      :wq

                      Comment

                      • Richard Heathfield

                        #12
                        Re: K&amp;R2, exercise 6.4

                        Richard Tobin said:
                        In article <n-OdnX00htjwVYfVn Z2dnUVZ8tChnZ2d @bt.com>,
                        Richard Heathfield <rjh@see.sig.in validwrote:
                        >
                        >>pi++ = &i; yields a value
                        >>>
                        >> right ?
                        >
                        >>Right.
                        >
                        Er...
                        Oh, I meant the pi++ bit. Good spot. Of course, I was making the general
                        point "all expressions yield values" (which isn't quite true, because void
                        expressions don't yield values, but on the other hand void expressions
                        'ardly hever 'appen).

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • arnuld

                          #13
                          Re: K&amp;R2, exercise 6.4

                          On Fri, 02 May 2008 11:19:23 +0000, Richard Heathfield wrote:

                          Yes, very much so. pi++ changes the value stored in the pi object, and
                          has no effect whatsoever on i. pi[i++] is equivalent to *(pi + i++),
                          which modifies the i object (adding 1 to it) and which yields the prior
                          value of i for addition to pi, giving a pointer value which can be
                          dereferenced to access the object being pointed at.
                          yes, I understand it completely. I think things are pretty much different
                          with pointer to arrays of pointers. I will try to post an example in some
                          other thread and will keep this thread specific to exercise 6.4 of K&R2.


                          > you never do arrays in C unless you have memory-constraints
                          Don't I? I thought I used them quite a lot, actually.

                          Oh.. I forgot to add ++ in front of C



                          --

                          my email ID is @ the above address

                          Comment

                          • Richard Heathfield

                            #14
                            Re: K&amp;R2, exercise 6.4

                            arnuld said:
                            >On Fri, 02 May 2008 11:19:23 +0000, Richard Heathfield wrote:
                            >
                            >
                            >Yes, very much so. pi++ changes the value stored in the pi object, and
                            >has no effect whatsoever on i. pi[i++] is equivalent to *(pi + i++),
                            >which modifies the i object (adding 1 to it) and which yields the prior
                            >value of i for addition to pi, giving a pointer value which can be
                            >dereferenced to access the object being pointed at.
                            >
                            yes, I understand it completely. I think things are pretty much different
                            with pointer to arrays of pointers.
                            No, the same point (that you can only assign values to objects, not to
                            other values) applies just the same in all cases. C is very simple like
                            that.
                            I will try to post an example in some
                            other thread and will keep this thread specific to exercise 6.4 of K&R2.
                            >
                            >> you never do arrays in C unless you have memory-constraints
                            >
                            >Don't I? I thought I used them quite a lot, actually.
                            >
                            >
                            Oh.. I forgot to add ++ in front of C
                            <shrugI use arrays in C++, too.

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            Working...