Thread: Convert double to int for array size

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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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,268
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    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,336
    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