Thread: Initialising everything in an array as 0

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    145

    Initialising everything in an array as 0

    I want to initialise everything as zero, however my array is declared like this:

    Code:
    int test[x] = {0};
    where x is a non zero positive int, but I get the error message:

    58 C:\Documents and Settings\jb\My Documents\a.c variable-sized object may not be initialized
    58 C:\Documents and Settings\jb\My Documents\a.c [Warning] excess elements in array initializer
    58 C:\Documents and Settings\jb\My Documents\a.c [Warning] (near initialization for `test')

    However when i declare it using:

    Code:
    int test[5] = {0};
    it works fine, any ideas?

    Thanks

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Are you compiling as C++?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    the problem is that x is not constant... And obviously - it means it is not C but C++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, that's not the problem. When the compile sees something like
    Code:
    int array[5] = {0};
    it automatically fills in the zeros and turns that into
    Code:
    int array[5] = {0, 0, 0, 0, 0};
    But with a variable-sized array, of course, it can't do that. Hence the error.
    Code:
    58 C:\Documents and Settings\jb\My Documents\a.c variable-sized object may not be initialized
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Wiretron
    I want to initialise everything as zero, however my array is declared like this:

    Code:
    int test[x] = {0};
    where x is a non zero positive int, but I get the error message:

    58 C:\Documents and Settings\jb\My Documents\a.c variable-sized object may not be initialized
    58 C:\Documents and Settings\jb\My Documents\a.c [Warning] excess elements in array initializer
    58 C:\Documents and Settings\jb\My Documents\a.c [Warning] (near initialization for `test')

    However when i declare it using:

    Code:
    int test[5] = {0};
    it works fine, any ideas?

    Thanks
    Your problem description is inconsistent. If you replace x with 5 (a non zero positive int) then the line that supposedly doesn't work is the same as the one that does. Please correct your question.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Oh I see how your question is wrong. x is not a non-zero positive int. It is in fact as written - a variable of type int that has been assigned a non-zero, positive value.
    You cannot do this when declaring an array.

  7. #7
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    His question is not wrong. The answers (except from dwks) are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM