Memory help?

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

    Memory help?

    int main(int argc ,char **argv)
    {
    char p[100] = "welcome";
    printf("%s", p) // prints welcome
    printf("%d", &p); prints addr of p
    printf("%c", *p); // prints 'w'
    }

    if p contains the value how come by referencing it gives the first
    char in string
  • santosh

    #2
    Re: Memory help?

    raashid bhatt wrote:
    int main(int argc ,char **argv)
    {
    char p[100] = "welcome";
    printf("%s", p) // prints welcome
    printf("%d", &p); prints addr of p
    No. Invokes undefined behviour since the expected and actual type of 'p'
    are different. To print pointer values use the %p conversion specifier:

    printf("%p\n", (void *)&p);

    The cast to void* is essential.
    printf("%c", *p); // prints 'w'
    }
    >
    if p contains the value how come by referencing it gives the first
    char in string
    Because in C strings are simply arrays of char and de-referencing the
    pointer to the first element of the array 'p' gives you... you guessed
    it, the character stored there.

    So 'p' is an array object that contains the string "welcome", but when
    you use the name of the array, in most contexts it is implicitly
    converted to a pointer value to it's first element (which in this case
    is a char) and the %c conversion specifier looks for a char* and prints
    the character that it references.

    You may want to look at the arrays and pointers section of the CLC FAQ
    at <http://www.c-faq.com/>

    Comment

    • Richard Tobin

      #3
      Re: Memory help?

      In article <5adad8ba-078a-4a36-a621-a79cf0543aee@b3 0g2000prf.googl egroups.com>,
      raashid bhatt <raashidbhatt@g mail.comwrote:
      >int main(int argc ,char **argv)
      >{
      char p[100] = "welcome";
      printf("%s", p) // prints welcome
      printf("%d", &p); prints addr of p
      This is wrong: an address is not an integer and may well be a different
      size. Use printf("%p", (void *)&p).
      printf("%c", *p); // prints 'w'
      >}
      >if p contains the value how come by referencing it gives the first
      >char in string
      p is the name of the array and in most contexts that turns into the
      address of the start of the array - a pointer to the array's first
      element. When you printed it with %s, it took the pointer and printed
      the characters starting at that address, up to the terminating null.
      When you dereferenced it with *p, you got the single character
      pointed to by p - the first character of the string.

      You might ask what the difference is between p and &p. Both are the
      address of the start of the array. But p has type pointer-to-char,
      and &p has type pointer-to-array-of-100-chars.

      -- Richard
      --
      Please remember to mention me / in tapes you leave behind.

      Comment

      • Ben Bacarisse

        #4
        Re: Memory help?

        santosh <santosh.k83@gm ail.comwrites:
        raashid bhatt wrote:
        <snip>
        > char p[100] = "welcome";
        > printf("%s", p) // prints welcome
        <snip>
        No. Invokes undefined behviour since the expected and actual type of 'p'
        are different. To print pointer values use the %p conversion specifier:
        >
        printf("%p\n", (void *)&p);
        Personally, I would not take the address. There are few "general
        rules" about C, but one I like to bear in mind is that code that does
        X with an array often ends up in a function and has to do X but with a
        pointer to the first element. I have got into the habit of having
        another look at everything array-ish, and wondering if it will
        survive, unchanged, a move into a context where the array is now just
        a pointer.

        --
        Ben.

        Comment

        • Thad Smith

          #5
          Re: Memory help?

          santosh wrote:
          raashid bhatt wrote:
          >
          > printf("%c", *p); // prints 'w'
          >if p contains the value how come by referencing it gives the first
          >char in string
          >
          Because in C strings are simply arrays of char and de-referencing the
          pointer to the first element of the array 'p' gives you... you guessed
          it, the character stored there.
          >
          So 'p' is an array object that contains the string "welcome", but when
          you use the name of the array, in most contexts it is implicitly
          converted to a pointer value to it's first element (which in this case
          is a char) and the %c conversion specifier looks for a char* and prints
          the character that it references.
          ITYM the %c specifier looks for an argument of type int, the writes the
          character (char)(unsigned char)argument_v alue.

          --
          Thad

          Comment

          • raashid bhatt

            #6
            Re: Memory help?

            You might ask what the difference is between p and &p.  Both are the
            address of the start of the array.  But p has type pointer-to-char,
            and &p has type pointer-to-array-of-100-chars.
            >
            you are wrong p and &p arent the same p points to the address where
            the value is stored is "welcome" , and &p contains the address of
            pointer itself

            Comment

            • Ben Bacarisse

              #7
              Re: Memory help?

              raashid bhatt <raashidbhatt@g mail.comwrites:

              context:
              char p[100] = "welcome";
              >You might ask what the difference is between p and &p.  Both are the
              >address of the start of the array.  But p has type pointer-to-char,
              >and &p has type pointer-to-array-of-100-chars.
              >
              you are wrong p and &p arent the same p points to the address where
              the value is stored is "welcome" , and &p contains the address of
              pointer itself
              One statement is dubious because of an eminently excusable language
              problem. If you'd said "points to the location where the value is
              stored" there would be no problem with it, but in C "address" means
              something very much like "pointer", so it is better to speak of a
              "pointer to a location" rather than a "pointer to an address".

              The second statement is simply wrong. How many people will have to
              say this before you start to doubt your understanding?

              By the way, don't clip attributions. The (entirely correct) text you
              quote was written by Richard Tobin.

              --
              Ben.

              Comment

              • Richard Bos

                #8
                Re: Memory help?

                raashid bhatt <raashidbhatt@g mail.comwrote:
                you are wrong
                You do not know nearly enough to make that judgement.

                Richard

                Comment

                • James Kuyper

                  #9
                  Re: Memory help?

                  raashid bhatt wrote:
                  >You might ask what the difference is between p and &p. Both are the
                  >address of the start of the array. But p has type pointer-to-char,
                  >and &p has type pointer-to-array-of-100-chars.
                  >>
                  >
                  you are wrong p and &p arent the same p points to the address where
                  the value is stored is "welcome" , and &p contains the address of
                  pointer itself
                  You're being confused, I think, by the fact that in most circumstances
                  an expression that has an array type is implicitly converted to a
                  pointer to the first element of the array. For instance:

                  char *q = p;

                  Since 'p' gets converted into a 'char*' in the above context, it might
                  therefore seem as though the type of &p should be char**. This would
                  imply that the implementation has to create an unnamed pointer to that
                  array somewhere in user memory, and the give you a pointer to that
                  pointer. However, being the right operand of '&' is one of the three
                  contexts where an expression of array type does not get implicitly
                  converted to a pointer to the first element of the array (see
                  6.3.2.1p3). In this context, p refers to the entire array, and applying
                  the '&' operator to p gives you a pointer to the entire array, not a
                  pointer to a pointer. 6.5.3.2p3 says 'If the operand has type "_type_",
                  the result has type "pointer to _type".' Since the type of 'p' is
                  "char[100]", the type of &p is "char(*)[100]", not char**.

                  Comment

                  • pete

                    #10
                    Re: Memory help?

                    raashid bhatt wrote:
                    >You might ask what the difference is between p and &p. Both are the
                    >address of the start of the array. But p has type pointer-to-char,
                    >and &p has type pointer-to-array-of-100-chars.
                    >>
                    >
                    you are wrong p and &p arent the same p points to the address where
                    the value is stored is "welcome" , and &p contains the address of
                    pointer itself
                    No.
                    (p) is an array.
                    In most contexts, (p) is converted to a pointer.
                    In this context:
                    (&p)
                    (p) is not converted to a pointer
                    and (&p) is the address of the array,
                    not the address of a pointer.




                    N869
                    6.3.2.1 Lvalues and function designators

                    [#3] Except when it is the operand of the sizeof operator or
                    the unary & operator, or is a string literal used to
                    initialize an array, an expression that has type ``array of
                    type'' is converted to an expression with type ``pointer to
                    type'' that points to the initial element of the array
                    object and is not an lvalue.


                    --
                    pete

                    Comment

                    Working...