String function problem

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

    String function problem

    Hello everybody, I am new to the C language can anybody tell me the
    diffrence between
    char nm[10] and char *nm ?
  • Walter Roberson

    #2
    Re: String function problem

    In article <f9e301c2-880a-44f2-a537-e20cfa602dfc@p3 9g2000prm.googl egroups.com>,
    Vijay <k.anurag87@yah oo.comwrote:
    >Hello everybody, I am new to the C language can anybody tell me the
    >diffrence between
    >char nm[10] and char *nm ?
    char nm[10]

    declares that nm is a variable in which 10 char can be stored.
    Space for those 10 characters will be automatically allocated.
    You can "really" store characters in the array nm.


    char *nm

    declares that nm is a variable which can be set to point
    to a character and then used to access characters pointed to.
    But the only space allocated by the statement is space for the
    pointer itself, and that pointer is not initialized to anything
    in particular, so until you cause nm to point to some real storage,
    this nm cannot be used to access anything.


    The above analysis does not apply if these are found in parameter
    lists in function declarations: in parameter lists, there is no
    difference in meaning, with both meaning that nm is of type
    pointer to char.
    --
    "There's no term to the work of a scientist." -- Walter Reisch

    Comment

    • Keith Thompson

      #3
      Re: String function problem

      Vijay <k.anurag87@yah oo.comwrites:
      Hello everybody, I am new to the C language can anybody tell me the
      diffrence between
      char nm[10] and char *nm ?
      The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 6 covers
      arrays and pointers.

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

      Comment

      • rahul

        #4
        Re: String function problem

        On Jun 10, 11:11 am, Vijay <k.anura...@yah oo.comwrote:
        Hello everybody, I am new to the C language can anybody tell me the
        diffrence between
        char nm[10] and char *nm ?
        char foo[10] defines an array of 10 characters. char *bar defines bar
        as a pointer to a character.
        Array name without any indexes is the pointer to the first element of
        the array. foo points to the first
        element i.e foo == &foo[0]. When passed to a function, the array
        decays to pointer to first element.
        bar can be dynamically allocated memory to contain more than one
        characters.
        The difference between foo and bar is foo is address of linearly
        allocated memory while bar, after mem allocation,
        contains the address of linearly allocated memory.

        Comment

        • pete

          #5
          Re: String function problem

          rahul wrote:
          Array name without any indexes is the pointer to the first element of
          the array.
          There's three exceptions to that:

          N869
          6.3.2 Other operands
          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. If the array object has
          register storage class, the behavior is undefined.

          --
          pete

          Comment

          • Jens Thoms Toerring

            #6
            Re: String function problem

            rahul <rahulsinner@gm ail.comwrote:
            On Jun 10, 11:11 am, Vijay <k.anura...@yah oo.comwrote:
            Hello everybody, I am new to the C language can anybody tell me the
            diffrence between
            char nm[10] and char *nm ?
            char foo[10] defines an array of 10 characters. char *bar defines bar
            as a pointer to a character.
            Array name without any indexes is the pointer to the first element of
            the array.
            Not really, an unadored array name stands for (not is) a pointer
            to the first element of the array in a context where a value is
            required. For example in

            int a[10];
            int *b = malloc( 100 );

            a = b;

            'a' is not a pointer. First reason is that 'a' appears not in
            a position where a value is allowed. Here you need something
            you can assign a value to. Second, if 'a' would be a pointer
            the assignment would be correct (you can assign a new value
            to a pointer) and you could actually change 'a' by assigning
            it a new value and suddenly '*a' would be the first value in
            the memory 'b' points to while 'a[0]' would still be the value
            of the first element of the array. Only someone with a strong
            interest in the obfuscated C contest (or a masochist) would
            truely enjoy that, I guess;-)

            Regards, Jens
            --
            \ Jens Thoms Toerring ___ jt@toerring.de
            \______________ ____________ http://toerring.de

            Comment

            • pete

              #7
              Re: String function problem

              Jens Thoms Toerring wrote:
              rahul <rahulsinner@gm ail.comwrote:
              >On Jun 10, 11:11 am, Vijay <k.anura...@yah oo.comwrote:
              >>Hello everybody, I am new to the C language can anybody tell me the
              >>diffrence between
              >>char nm[10] and char *nm ?
              >
              >char foo[10] defines an array of 10 characters. char *bar defines bar
              >as a pointer to a character.
              >
              >Array name without any indexes is the pointer to the first element of
              >the array.
              >
              Not really, an unadored array name stands for (not is) a pointer
              to the first element of the array in a context where a value is
              required.
              That's not the rule.
              The implicit conversion is specified
              as taking place always, with three exceptions.
              For example in
              >
              int a[10];
              int *b = malloc( 100 );
              >
              a = b;
              >
              'a' is not a pointer.
              (a) is converted to a pointer type in that context.
              First reason is that 'a' appears not in
              a position where a value is allowed. Here you need something
              you can assign a value to. Second, if 'a' would be a pointer
              the assignment would be correct (you can assign a new value
              to a pointer) and you could actually change 'a' by assigning
              it a new value and suddenly '*a' would be the first value in
              the memory 'b' points to while 'a[0]' would still be the value
              of the first element of the array.
              That's wrong.
              The standard explicitly states that an array type is not a
              "modifiable lvalue", however, even if it didn't:
              The conversion of the array type expression to a pointer type,
              makes the resulting expression not an lvalue,
              which is sufficient to disallow an array type from appearing
              as a left assignment operand.


              6.3.2 Other operands
              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.

              6.5.16 Assignment operators

              [#2] An assignment operator shall have a modifiable lvalue
              as its left operand.


              --
              pete

              Comment

              Working...