String function problem

Discussion in 'C Programming' started by Vijay, Jun 10, 2008.

  1. Vijay

    Vijay Guest

    Hello everybody, I am new to the C language can anybody tell me the
    diffrence between
    char nm[10] and char *nm ?
     
    Vijay, Jun 10, 2008
    #1
    1. Advertising

  2. In article <>,
    Vijay <> wrote:
    >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
     
    Walter Roberson, Jun 10, 2008
    #2
    1. Advertising

  3. Vijay <> writes:
    > 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_Keith) <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"
     
    Keith Thompson, Jun 10, 2008
    #3
  4. Vijay

    rahul Guest

    On Jun 10, 11:11 am, Vijay <> wrote:
    > 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.
     
    rahul, Jun 10, 2008
    #4
  5. rahul <> wrote:
    > On Jun 10, 11:11 am, Vijay <> wrote:
    > > 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 ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,060
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,435
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    939
    Bengt Richter
    Aug 1, 2005
  4. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,250
    Barry Schwarz
    Sep 5, 2005
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Replies:
    5
    Views:
    1,307
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 6, 2006
Loading...

Share This Page