arrays

Discussion in 'C Programming' started by mdh, May 14, 2006.

  1. mdh

    mdh Guest

    I am still early on with K&R, but every now and again, I try to assign
    to an array like this:

    char arr[]="I am a string"; /* works*/

    but

    char arr[MAX]; /* MAX == some number */

    char arr[]="I am a string"; / * Does not work */

    or arr[]="I am a string"; / * Does not work */

    Not done pointers yet. Could someone explain what is going on.

    Thanks in advance.
     
    mdh, May 14, 2006
    #1
    1. Advertising

  2. mdh wrote:
    > I am still early on with K&R, but every now and again, I try to assign
    > to an array like this:
    >
    > char arr[]="I am a string"; /* works*/


    This is not an assignment but an initialization.

    > but
    >
    > char arr[MAX]; /* MAX == some number */
    >
    > char arr[]="I am a string"; / * Does not work */


    This is an attempt to define a variable twice in the same scope.

    > or arr[]="I am a string"; / * Does not work */


    arr[] is not a valid expression for the left side of an assignment.

    > Not done pointers yet. Could someone explain what is going on.


    There is special syntax to initialize arrays, i.e. to provide
    values on definition. For Example:

    int arr[] = { 72, 45, 76, 76, 79 };

    or

    int arr[] = { 'H', 'E', 'L', 'L', 'O' };

    For the special case of an array of characters there is the very
    special syntax to initialize with a string literal, e.g:

    char arr[] = "HELLO";

    This will define an array of six char, the last item being a 0.
    To omit this terminating zero you can specify exact array size:

    char arr[5] = "HELLO";

    In contrast, assignment of arrays involves pointers.

    --
    Ich kenne die Mißverständnisse-FAQ, und sie wird oft mißverstanden.
    -- Andreas M. Kirchwitz <>
     
    Alexander Bartolich, May 14, 2006
    #2
    1. Advertising

  3. On 2006-05-14, mdh <> wrote:
    > I am still early on with K&R, but every now and again, I try to assign
    > to an array like this:
    >
    > char arr[]="I am a string"; /* works*/
    >

    Valid C.

    > but
    >
    > char arr[MAX]; /* MAX == some number */
    >

    Now arr points to an array of char or size MAX. Remember though that
    arr is /not/ a pointer; it merely behaves similarly.

    > char arr[]="I am a string"; / * Does not work */

    If you've already defined arr, "char arr" is a double definition.

    > or arr[]="I am a string"; / * Does not work */

    If you've already defined arr to point to an array of size MAX, when
    you attempt to point it to a string literal of size 14, it will bomb.
    Remember, the address of your array is different from the address of
    a string literal.

    Basically: String literals are pointers. Arrays are not.

    (Also, "arr[] =" is invalid.)

    > Not done pointers yet. Could someone explain what is going on.
    >

    You're going to need to finish pointers before you can do too much C.

    > Thanks in advance.
    >
     
    Andrew Poelstra, May 14, 2006
    #3
  4. mdh

    mdh Guest

    Andrew Poelstra wrote:
    > You're going to need to finish pointers before you can do too much C.



    I agree, but this will help.

    Thanks to both of you.
     
    mdh, May 14, 2006
    #4
  5. mdh said:

    > I am still early on with K&R, but every now and again, I try to assign
    > to an array like this:
    >
    > char arr[]="I am a string"; /* works*/


    Yes.

    >
    > but
    >
    > char arr[MAX]; /* MAX == some number */
    >
    > char arr[]="I am a string"; / * Does not work */


    No, because you already defined it. But:

    char arr[MAX] = "I am a string"; /* works */

    > or arr[]="I am a string"; / * Does not work */
    >
    > Not done pointers yet. Could someone explain what is going on.


    As you have discovered, you can initialise arrays (i.e. give them a value at
    the same time that you define them), but you can't assign to them.

    But you /can/ assign to their members. That's how string functions work - by
    dealing with the individual elements in the array, rather than the whole
    array. So:

    #include <string.h>

    #define MAX 32

    int main(void)
    {
    char arr[MAX];
    strcpy(arr, "I am a string"); /* works */
    return 0;
    }

    Just keep plugging away with K&R - all will become clear(er).

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 14, 2006
    #5
  6. mdh

    mdh Guest

    Richard Heathfield wrote:
    >> Just keep plugging away with K&R - all will become clear(er).



    thanks...I will...but this board's encouragement certainly helps.
     
    mdh, May 14, 2006
    #6
  7. Andrew Poelstra said:

    >> char arr[MAX]; /* MAX == some number */
    >>

    > Now arr points to an array of char or size MAX.


    No, it doesn't. arr is an array, not a pointer. It doesn't point anywhere.

    > Basically: String literals are pointers.


    No, a string literal with N characters is an array of N + 1 char.

    > Arrays are not [pointers].


    Right - so they don't point, do they?

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, May 14, 2006
    #7
  8. mdh

    CBFalconer Guest

    mdh wrote:
    > Richard Heathfield wrote:
    >
    >> Just keep plugging away with K&R - all will become clear(er).

    >
    > thanks...I will...but this board's encouragement certainly helps.


    Just to keep things clear, 'this' is not a board, but a flawed
    interface to the worldwide usenet news system. This allows you to
    exchange knowledge with people all over the world, and is a system
    that has existed for something like 25 years.

    Congratulations on using the google interface intelligently.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
     
    CBFalconer, May 14, 2006
    #8
  9. mdh

    Denis Kasak Guest

    mdh wrote:
    > I am still early on with K&R, but every now and again, I try to assign
    > to an array like this:
    >
    > char arr[]="I am a string"; /* works*/


    Here you are defining an array 'arr' whose elements are of type char.
    The part after the '=' character denotes initialisation. You can
    initialise arrays with content while defining them by using the syntax
    above. Note that this is *not* an assignment.

    > but
    >
    > char arr[MAX]; /* MAX == some number */


    You are defining an array of MAX chars which is named 'arr'.

    > char arr[]="I am a string"; / * Does not work */


    You are attempting to redefine the identifier 'arr'. This is not allowed
    because you have already defined it in the current scope.

    > or arr[]="I am a string"; / * Does not work */


    Now you may wonder why this doesn't work when your first example does.
    The reason for this is that, even though the same '=' character is used
    here as in your first example, it doesn't have the same meaning. Here it
    denotes the assigment operator and arrays can't be assigned to.

    The proper way of copying "I am a string" into arr[] would be by using
    strcpy().

    > Not done pointers yet. Could someone explain what is going on.


    Just keep on reading and you will reach enlightement.

    --
    Denis Kasak
     
    Denis Kasak, May 14, 2006
    #9
  10. mdh

    pete Guest

    Andrew Poelstra wrote:
    >
    > On 2006-05-14, mdh <> wrote:


    > > char arr[]="I am a string"; /* works*/


    > Basically: String literals are pointers. Arrays are not.


    There is no pointer in the line of code quoted above.

    --
    pete
     
    pete, May 15, 2006
    #10
  11. On 2006-05-15, pete <> wrote:
    > Andrew Poelstra wrote:
    >>
    >> On 2006-05-14, mdh <> wrote:

    >
    >> > char arr[]="I am a string"; /* works*/

    >
    >> Basically: String literals are pointers. Arrays are not.

    >
    > There is no pointer in the line of code quoted above.
    >

    I swear I read somewhere that string literals and/or arrays are pointers.
    I've gotta get TYC21 out of my head.

    An array name points to its first element. String literals have addresses,
    which is what is pointed to by 'char *str = "This is a string.";'

    I appreciate that arrays and pointers are different, but I've never
    understood /why/ we make that distinction, other than in 3 specific cases.
     
    Andrew Poelstra, May 15, 2006
    #11
  12. mdh

    Flash Gordon Guest

    Andrew Poelstra wrote:
    > On 2006-05-15, pete <> wrote:
    >> Andrew Poelstra wrote:
    >>> On 2006-05-14, mdh <> wrote:
    >>>> char arr[]="I am a string"; /* works*/
    >>> Basically: String literals are pointers. Arrays are not.

    >> There is no pointer in the line of code quoted above.
    >>

    > I swear I read somewhere that string literals and/or arrays are pointers.
    > I've gotta get TYC21 out of my head.
    >
    > An array name points to its first element.


    No, it refers to the entire array, it just happens to decay to a pointer
    to the first element most of the time.

    > String literals have addresses,


    So does an int.

    > which is what is pointed to by 'char *str = "This is a string.";'
    >
    > I appreciate that arrays and pointers are different, but I've never
    > understood /why/ we make that distinction, other than in 3 specific cases.


    You mean as operands of &, sizeof, or as an initialiser for an array?
    How about assignment operators? Or increment/decrement operators?

    How about:
    {
    char *p;
    char a[10];
    p[0] = 'x';
    a[0] = 'x';
    }
    Does the distinction matter in the above code fragment?

    The distinction is made so heavily a around here because so many
    mistakes are made by people due to them not understanding the distinction.

    See section 6 of the comp.lang.c FAQ at http://c-faq.com/ and consider
    not only the answers but why the questions ended up being in the FAQ in
    the first place, noting the FAQ stand for *Frequently* Asked Questions.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, May 15, 2006
    #12
  13. On 2006-05-15, Flash Gordon <> wrote:
    > Andrew Poelstra wrote:
    >> On 2006-05-15, pete <> wrote:
    >>> Andrew Poelstra wrote:
    >>>> On 2006-05-14, mdh <> wrote:
    >>>>> char arr[]="I am a string"; /* works*/
    >>>> Basically: String literals are pointers. Arrays are not.
    >>> There is no pointer in the line of code quoted above.
    >>>

    >> I swear I read somewhere that string literals and/or arrays are pointers.
    >> I've gotta get TYC21 out of my head.
    >>
    >> An array name points to its first element.

    >
    > No, it refers to the entire array, it just happens to decay to a pointer
    > to the first element most of the time.
    >
    > > String literals have addresses,

    >
    > So does an int.
    >
    >> which is what is pointed to by 'char *str = "This is a string.";'
    >>
    >> I appreciate that arrays and pointers are different, but I've never
    >> understood /why/ we make that distinction, other than in 3 specific cases.

    >
    > You mean as operands of &, sizeof, or as an initialiser for an array?
    > How about assignment operators? Or increment/decrement operators?
    >
    > How about:
    > {
    > char *p;
    > char a[10];
    > p[0] = 'x';
    > a[0] = 'x';
    > }
    > Does the distinction matter in the above code fragment?
    >

    Well, yeah, because p has not been allocated.

    > The distinction is made so heavily a around here because so many
    > mistakes are made by people due to them not understanding the distinction.
    >

    I can understand that the above might not be obvious to a beginner.

    > See section 6 of the comp.lang.c FAQ at http://c-faq.com/ and consider
    > not only the answers but why the questions ended up being in the FAQ in
    > the first place, noting the FAQ stand for *Frequently* Asked Questions.

    Thank you for your informative and kind answer.
     
    Andrew Poelstra, May 15, 2006
    #13
  14. On Mon, 15 May 2006 14:26:11 UTC, Andrew Poelstra
    <> wrote:

    > On 2006-05-15, pete <> wrote:
    > > Andrew Poelstra wrote:
    > >>
    > >> On 2006-05-14, mdh <> wrote:

    > >
    > >> > char arr[]="I am a string"; /* works*/

    > >
    > >> Basically: String literals are pointers. Arrays are not.

    > >
    > > There is no pointer in the line of code quoted above.
    > >

    > I swear I read somewhere that string literals and/or arrays are pointers.
    > I've gotta get TYC21 out of my head.


    Maybe, there are enough idiots around who are unable to understund C.

    An array name is NOT a pointer but can decide behave like one in some
    contect.

    > An array name points to its first element. String literals have addresses,
    > which is what is pointed to by 'char *str = "This is a string.";'


    True, but nothing makes them to be a pointer. A string literal is an
    array not a pointer. An array can decite to deliver its address in
    pointer context but it is noways an pointer on itself.

    > I appreciate that arrays and pointers are different, but I've never
    > understood /why/ we make that distinction, other than in 3 specific cases.


    An array is the name of the base of its sequence of objects. A string
    literal is an unnamed array, both are able to deliver its base address
    (aka pointer to its first object) but they are nowasy a pointer.

    --
    Tschau/Bye
    Herbert

    Visit http://www.ecomstation.de the home of german eComStation
    eComStation 1.2 Deutsch ist da!
     
    Herbert Rosenau, May 16, 2006
    #14
  15. "Herbert Rosenau" <> writes:
    [...]
    > An array is the name of the base of its sequence of objects. A string
    > literal is an unnamed array, both are able to deliver its base address
    > (aka pointer to its first object) but they are nowasy a pointer.


    Given:
    int arr[42];
    arr is the name of the entire array object, not just of its base.
    For example, "sizeof arr" yields the size of the entire object
    (42*sizeof(int)).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 16, 2006
    #15
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,653
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,259
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    569
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    386
  5. Philipp
    Replies:
    21
    Views:
    1,130
    Philipp
    Jan 20, 2009
Loading...

Share This Page