Thread: Array Defining Problem

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Array Defining Problem

    Ok ....I am new to C programming.....
    Till now i m not having any problems with coding....
    But i m confused with theory....
    I m referring Gottfried for C programming....And in that book it states that "Automatic Array cannot be initialized"...But when i try to make a simple program like
    insert
    Code:
    main()
    {
    char y[]="Is this automatic array";
    }
    there is no compiling problem....

    Or is it that y[] turns out to be static by default????

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    first off, you need to include the headers, second declare MAIN the right way, and have a return to end your program....start there!

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    45
    Quote Originally Posted by psyduck View Post
    [..] in that book it states that "Automatic Array cannot be initialized"...[..]

    Really? Doesn't it say something like "automatic arrays (and any other automatic variables) may contain garbage if left uninitialized" or something similar?


    Can you copy the full paragraph, please?


    When was that book written?


    Quote Originally Posted by psyduck View Post
    Or is it that y[] turns out to be static by default????

    No, it doesn't.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by comocomocomo View Post
    Really? Doesn't it say something like "automatic arrays (and any other automatic variables) may contain garbage if left uninitialized" or something similar?


    Can you copy the full paragraph, please?


    When was that book written?





    No, it doesn't.
    "Automatic Arrays,unlike automatic variables,cannot be initialized.However,external and static array definitions can include the assignment of initial values if desired.The initial values must appear in the order in which they will be assigned to the individual array elements enclosed in braces and separated by commas."

    Book was written in 1990...i think there must be some change in ANSI standards....
    And ya it does say automatic variables are left with garbage values if not intialized and it is an unintelligentable act

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Hmm, well what you're reading wasn't true in 1990 either. In your example, y is initialized.

    this or anything quite similar is also initialization:
    Code:
    char y[] = {'a', 'u', 't', 'o', 'm', 'a', 't', 'i', 'c', '\0'};
    Just ain't so.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    I think you need a better book.
    The statement about arrays is flat out wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Quote Originally Posted by Salem View Post
    I think you need a better book.
    The statement about arrays is flat out wrong.
    The language of the book is quiet good...So i m referring it...And at places i m referring Internet too...if by chance there is any change in 25 years

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The standard changed significantly around 1999. It changed again quite recently. You really do need a better book. There is no shortage on that.

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    Thanks

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by psyduck View Post
    The language of the book is quiet good...So i m referring it...And at places i m referring Internet too...if by chance there is any change in 25 years
    The particular statement you asked about is wrong. It has never been correct in Standard C. It was also incorrect in K&R C (the first language known as C, which was a precursor of C as first standardised in 1989). Initialisation of local (auto) arrays has always been permitted.

    There are plenty of books and plenty of internet pages that provide incorrect information about C in a readable manner. Apparently you have one of them.
    Last edited by grumpy; 12-28-2012 at 03:24 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-21-2012, 01:14 AM
  2. doubt while defining array and pointer
    By karthik537 in forum C Programming
    Replies: 4
    Last Post: 08-24-2010, 12:05 AM
  3. problem defining an array :s
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 12-23-2008, 11:16 AM
  4. Difficulty With Defining an Array
    By dirtbiker in forum C Programming
    Replies: 9
    Last Post: 11-10-2005, 05:51 AM
  5. Defining an Array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2002, 01:16 PM