Convert double to int for array size

This is a discussion on Convert double to int for array size within the C Programming forums, part of the General Programming Boards category; Hi, I want to declare an array size based on a double calculation. Rounding it up or down doesn't really ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    Convert double to int for array size

    Hi,

    I want to declare an array size based on a double calculation. Rounding it up or down doesn't really matter. As the array size needs to be an integer, is there any way I can convert the double result to an integer?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    i = (int)d;

    Otherwise known as "look it up".
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Rounding it up or down probably does matter. Depending on round-off error and all that good stuff. If the result was very close to an integer (meant to be an integer but could be +/- some tiny fraction) you have to find the closest integer. Usually something like
    Code:
    i = (int)(d + 0.5);

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    I need to use the int value to declare the array size. However when I try the suggested responses it does not work.

    It says "integral constant expression expected"


    double z = x/y;
    int a = (int) z;

    int array1 = [a];

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    int array1 = [a];
    That is not how you declare an array. You need to pick up a book, or read through some tutorials online.

    At any rate, try this:
    Code:
    int array1[a];
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Quote Originally Posted by bithub View Post
    Code:
    int array1 = [a];
    That is not how you declare an array. You need to pick up a book, or read through some tutorials online.

    At any rate, try this:
    Code:
    int array1[a];
    My apologies, that was a typo. However the code with the correct syntax is not working.

    double z = x/y;
    int a = (int) z;

    int array1[a];


    I'm still getting "integral constant expression expected".

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    Quote Originally Posted by millsy5
    I'm still getting "integral constant expression expected".
    That is because unless you are compiling with respect to C99, this is wrong:
    Code:
    int a = (int) z;
    
    int array1[a];
    You are trying to create a variable length array, and that is not allowed in standard C prior to the 1999 edition of the C standard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Epy
    Epy is online now
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    check out malloc:

    #include <stdlib.h>

    int *array1 = NULL;
    array1 = malloc(a * sizeof(int));

    memory allocated by malloc should be later freed and the pointer set to null:

    free(array1);
    array1 = NULL;

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    12
    Quote Originally Posted by laserlight View Post
    That is because unless you are compiling with respect to C99, this is wrong:
    Code:
    int a = (int) z;
    
    int array1[a];
    You are trying to create a variable length array, and that is not allowed in standard C prior to the 1999 edition of the C standard.
    Thanks. How do I check which compiler I am using?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by millsy5 View Post
    Thanks. How do I check which compiler I am using?
    By ... how you run the compiler? If you type "gcc" you're using gcc, et cetera. If you push a button, then you should know what program you downloaded that has a button you can push, and that program will tell you what the compiler is.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

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