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

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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
    715
    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, 10: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, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM