Doubt about array's name

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

    Doubt about array's name

    In the following function, s shouldn't be a pointer costant (array's name)?

    So why it is legal its increment? Thanks in advance.

    /* Code starts here */
    void chartobyte (char *s) {

    while (s!=0) {
    printf ("%d", *s);
    s++;
    }
    /* Code ends here */
  • Richard Heathfield

    #2
    Re: Doubt about array's name

    nembo kid said:
    In the following function, [shouldn't s] be a pointer costant (array's
    name)?
    It's just a copy. C is pass-by-value. When the argument expression is
    evaluated (in the *call* to chartobyte()), the array name is treated as if
    it were a pointer to the first element in the array, and this pointer
    value is copied into the parameter object that the implementation
    constructs for the call.
    So why it is legal its increment?
    Why not? It's only a copy, after all. It doesn't affect the original array
    address in any way whatsoever.
    /* Code starts here */
    void chartobyte (char *s) {
    >
    while (s!=0) {
    printf ("%d", *s);
    s++;
    }
    /* Code ends here */
    I think you meant to write:

    void chartobyte(cons t char *s) /* significant difference #1 */
    {
    while(*s != '\0') /* significant difference #2 */
    {
    printf("%d", *s);
    s++;
    }
    }

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

    • Ofloo

      #3
      Re: Doubt about array's name

      On 30 apr, 15:34, nembo kid <u...@localhost .comwrote:
      In the following function, s shouldn't be a pointer costant (array's name)?
      >
      So why it is legal its increment? Thanks in advance.
      >
      /* Code starts here */
      void chartobyte (char *s) {
      >
      while (s!=0) {
      printf ("%d", *s);
      s++;}
      >
      /* Code ends here */
      From what i understood from arrays is that array[0] == *array and
      array[1] == array++; *array .. and so forth if the predefined size of
      the pointer is 4bytes array++ will go to the next 4bytes in memory ..
      basicly pointers are arrays and arrays are pointers, .. the other
      thing should work also the other way arround, ..

      #include <stdio.h>
      #include <string.h>

      int main() {
      int i;
      char *p_str = "mystring";
      printf ("%s\n", p_str);
      for (i = 0;i < strlen(p_str); i++)
      printf ("%c", p_str[i]);
      printf ("\n");
      return 0;
      }

      Comment

      • nembo kid

        #4
        Re: Doubt about array's name

        Richard Heathfield ha scritto:
        Why not? It's only a copy, after all. It doesn't affect the original array
        address in any way whatsoever.
        Ok my doubts cleared all.

        But anyway I dont'understand because someone add the qualificator
        "const" to string array passed to the function (so to avoid any change
        of this pointer).

        /* Code */
        void chartobyte (const char *s)
        /* Code */

        while(*s != '\0') /* significant difference #2 */
        Yes, was my mistake.

        Thanks again and apologies for my bad english.

        Comment

        • Keith Thompson

          #5
          Re: Doubt about array's name

          nembo kid <user@localhost .comwrites:
          Richard Heathfield ha scritto:
          >Why not? It's only a copy, after all. It doesn't affect the original
          >array address in any way whatsoever.
          >
          Ok my doubts cleared all.
          >
          But anyway I dont'understand because someone add the qualificator
          "const" to string array passed to the function (so to avoid any change
          of this pointer).
          /* Code */
          void chartobyte (const char *s)
          /* Code */
          ``const char *s'' declares s as a pointer to const char, not as a
          const pointer to char. In other words, you're permitted to modify the
          pointer (since it's just a local copy), but you're not permitted to
          modify what it points to.

          If you wanted to declare a const pointer to char (so the pointer
          itself is read-only), you could write:

          char *const s;

          But function parameters, since they're purely local to the function,
          are not typically declared as "const", since any modification won't
          affect the caller anyway. You could declare a parameter as "const" if
          you don't intend to modify it within the function.

          --
          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Keith Thompson

            #6
            Re: Doubt about array's name

            Ofloo <info@ofloo.net writes:
            On 30 apr, 15:34, nembo kid <u...@localhost .comwrote:
            >In the following function, s shouldn't be a pointer costant (array's name)?
            >>
            >So why it is legal its increment? Thanks in advance.
            >>
            >/* Code starts here */
            >void chartobyte (char *s) {
            >>
            >while (s!=0) {
            > printf ("%d", *s);
            > s++;}
            >>
            >/* Code ends here */
            >
            From what i understood from arrays is that array[0] == *array and
            Yes, this follows from the definition of the [] operator.

            arr[i] means *(arr+i)

            This works because arr is an array expression and is therefore
            converted, in most but not all contexts, to a pointer to its first
            element.
            array[1] == array++;
            No. array[1] is equivalent to *(array+1). array++ is (a) of the
            wrong type, and (b) illegal, since ``array'' is not a modifiable
            lvalue.

            I've seen a tendency among some new C programmers to over-use the "++"
            operator. "x++" is not just a cool way to say "x + 1". It yields the
            original value of x and, as a side effect, modifies x.

            I think what you meant to say is that array[1] == array+1 -- but
            that's still incorrect, because array[1] is an element of the array,
            and array+1 is a *pointer* to that element.
            *array .. and so forth if the predefined size of
            the pointer is 4bytes array++ will go to the next 4bytes in memory ..
            basicly pointers are arrays and arrays are pointers, .. the other
            thing should work also the other way arround, ..
            That is one of the most common fundamental misunderstandin gs about C.
            Arrays are not pointers. Pointers are not arrays. Most operations on
            arrays are defined in terms of pointers, but they are still two
            entirely distinct things. It sometimes seems as if the language is
            designed to obscure this important distinction.

            The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read section 6,
            "Arrays and Pointers".

            The following code is basically sound, but I'm going to critique some
            of the details anyway.
            #include <stdio.h>
            #include <string.h>
            >
            int main() {
            Ok, but "int main(void)" is better.
            int i;
            char *p_str = "mystring";
            It's better to add a 'const' keyword here. You're not allowed to
            modify the contents of a string literal; using 'const' makes this
            explicit.
            printf ("%s\n", p_str);
            for (i = 0;i < strlen(p_str); i++)
            strlen() returns a result of type size_t. Though you certainly can
            store that result in an int, it's usually better to keep types
            consistent. In this case, for traversing a string, using int can't
            cause any actual problems unless the string is longer than 32767
            characters. But it's a good habit anyway.

            Note that size_t is an unsigned type, which can make using it a trifle
            more complicated. For example, if i is of type size_t, then a
            comparison like ``i >= 0'' (if you're counting down rather than up)
            won't work; an unsigned type is *always* >= 0.

            A more serious problem here is performance. strlen() has to scan from
            the beginning of the string to the terminating '\0' to determine its
            length; in computer science terms, it's an O(N) operation, where N is
            the length of the string. You evaluate strlen(p_str) every time
            through the loop (even though the length never changes), so the loop
            as a whole is O(N**2) (N squared). The effect isn't noticeable in a
            small program like this (more to the point, with a short string like
            "mystring") , but it can be serious in larger programs.

            Save the value of strlen(p_str) in a variable, and use that variable
            in the loop.
            printf ("%c", p_str[i]);
            printf ("\n");
            return 0;
            }
            --
            Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            Working...