Can I not do this or am I doing it wrong?

This is a discussion on Can I not do this or am I doing it wrong? within the C Programming forums, part of the General Programming Boards category; I'm trying to initialize an array with 10 floating point numbers in it. This would be the only way I ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    Can I not do this or am I doing it wrong?

    I'm trying to initialize an array with 10 floating point numbers in it. This would be the only way I know how. Please advise

    Code:
    float array[10] = (1.66,.58,-2.35,0,1.9,0,-.34,0,7.56,-.3);

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    I tried using 11 instead of 10 in case it needed room for the null character (forgot how that works) but that didn't help any.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Use { }, not ( )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    haha yeah I just figured that out thanks a lot

  5. #5
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    First you need {} instead of () when define an array.
    Null character is needed for arrays of type char that means strings.

  6. #6
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    - nm -
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Micko
    Null character is needed for arrays of type char that means strings.
    The wording of your sentence is odd here, and open to interpretation. Depending on your meaning, you are either right, or wrong:
    Code:
    char array[5] = { 'h', 'e', 'l', 'l', 'o' };
    The above is perfectly valid. The above character array has been fully initialized, and may be used as you see fit, provided you don't try to use it as a "string". You do not have to have the null character in character arrays. It is not a requirement of arrays of type char.

    However, if you do intend to use it as a C type string, you need to have it null terminated, because that is the definition of a C type string. (An array of characters terminated by a null.)
    Code:
    char array[] = "hello";
    This is a string because a null character is appended to it. You could use the same initialization on the above array, and no array would be appended, thus it would not be a string.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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