Thread: variable lenght arrays

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    29

    variable lenght arrays

    How do you prompt the user to enter the number of elements for the array and use that information to creatr a variable length array?
    And then how do you prompt the user to enter in a number for each element of the array and scan in the appropriate numbers? the numbers are double precision floating point.

    for example,
    Enter the numbe of elements in the array: 3

    Enter element 0: 3
    Enter element 1: -1
    Enter element 2: 4

    I know it starts with

    int main()
    {
    double N;
    int a[size];

    printf("Enter the number of elements in the array:" );
    scanf("%f", &size); //I'm pretty sure this is wrong

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    declare size as an int
    declare array of floats
    prompt user to enter in size of array
    scan in size
    for loop i = 0
    prompt user to enter in float for array value
    scan in array i
    loop back:
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    29
    Code:
    int main()
    {
    double a[];
    int n, i;
    printf("Enter the number of elements in an array: ");
    scanf("%d", &n);
    for (i=0; i<n; i++)
    {
        printf("Enter element %d: ", n);
        scanf("%d", &i);
    }
    Is this correct?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Let me be more precise, follow the steps in order
    Code:
    declare n as an int
    declare array of floats with n in the brackets
    prompt user to enter in size of array
    scan in n
    
    for loop i = 0
    prompt user to enter in float for array value
    scan in array i
    loop back:
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by camel-man View Post
    Let me be more precise, follow the steps in order
    Code:
    declare n as an int
    declare array of floats with n in the brackets
    prompt user to enter in size of array
    scan in n
    
    for loop i = 0
    prompt user to enter in float for array value
    scan in array i
    loop back:
    You have given a precise description of a method to achieve undefined behaviour.

    When declaring a VLA, using the syntax
    Code:
        int n = some_value_obtained_from_somewhere();
        float array[n];
    the result is an array with number of elements equal to the value of n at that time. There is no magical association established between the array and n. Subsequently changing n (for example, reading a new value from the user) therefore does NOT magically resize the array.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by Gkendoyan View Post
    Code:
    int main()
    {
    double a[]; /// For a VLA, the declaration should come after you have the length
    
    int n, i;
    printf("Enter the number of elements in an array: ");
    scanf("%d", &n);
    
    /// Array declaration goes here, include [n]
    
    for (i=0; i<n; i++)
    {
        printf("Enter element %d: ", n);
        scanf("%d", &i);  /// This is not a good idea, 'i' controls the loop
    
        /// Assign values to VLA around here
    }
    Is this correct?
    I would really recommend just using malloc() rather than a VLA, it's not as scary as it looks. You just have to remember that the value you give it is the number of bytes you will get in return (which is why you always see the sizeof operator being used with it). Aside from that you just need to remember the symbols associated with the pointers malloc() returns so you can free() it later.
    Last edited by Alpo; 10-24-2014 at 10:24 PM.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    What am I remembering.. for some reason I thought VLA were valid after C99.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    for some reason I thought VLA were valid after C99.
    No VLA are only guaranteed to be valid with C99. Before C99 they were not supported by the standard. After C99 they became optional, meaning a compiler no longer needs to support this feature (and several others) to claim standard compliance.

    If you are going to use this "feature" you should really find and study the documentation for this "feature". Remember VLA are limited to the stack space, as are ordinary statically allocated arrays. There are also differences between VLA and "normal" arrays. For example a VLA can not be declared static.

    Jim

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by camel-man View Post
    What am I remembering.. for some reason I thought VLA were valid after C99.
    As Jim said, the 1999 C standard was the first C standard that specified VLAs, but some compilers supported VLA's (or comparable extensions) before then.

    Your preceding description in this thread, however, of how to use VLAs is incorrect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable-Length Arrays
    By Quant89 in forum C Programming
    Replies: 1
    Last Post: 04-13-2013, 09:57 AM
  2. file name lenght for include directive
    By kpmkhaja in forum Windows Programming
    Replies: 12
    Last Post: 10-11-2010, 03:41 AM
  3. Variable length arrays
    By nik2 in forum C Programming
    Replies: 2
    Last Post: 03-18-2010, 09:48 AM
  4. Finding lenght on elements
    By Suchy in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2006, 03:38 PM
  5. minimum lenght of UDP data ...?
    By arian in forum Networking/Device Communication
    Replies: 8
    Last Post: 08-08-2005, 05:40 AM