Initialising everything in an array as 0

This is a discussion on Initialising everything in an array as 0 within the C Programming forums, part of the General Programming Boards category; I want to initialise everything as zero, however my array is declared like this: Code: int test[x] = {0}; where ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    the problem is that x is not constant... And obviously - it means it is not C but C++
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    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,308
    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,308
    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, 08:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11: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, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21