Thread: array creation from variable sizes

  1. #1
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120

    array creation from variable sizes

    ok sorry about posting on an old topic but if i was to have a program that read a number from a file then created an array from tha tnumber how would or could i aaccomplish this. an example is like this:
    assuming the file opened correctly and had no problems
    Code:
    fscanf(fPtr,"%d",&nHeigth);
    fscanf(fPtr,"%d",&nWidth);
    char chArray[nHeight][nWidth];
    why would this be such a problem, i get the old cannot allocate an array of size 0 and expected constant expression when i try it. i know that the information that it is reading in is correct because i already checked that. is there an easier way or a way that works? i am using vc for this particular project.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int nSize;
        printf("Enter the size: ");
        scanf("%d",&nSize);
        
        int arr[nSize];
        
        printf("%d\n",sizeof(arr));
        system("pause");
        return 0;
    }
    Works fine, so I think the problem is that you're getting a wrong value from the file...

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    ok, i can compile that code under Dev C++ and i made a similar program that read in from a file and then made an array and it also worked in Dev C++ but when i ran it in msvc it came up with the same problems, is there a way to get around this in msvc because i am using some of its features for windows programming not related to this topic and i know the entire program would not compile under Dev C++. how can i make this work in msvc i guess would be my ultimate question?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  4. #4
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    well i would think that the use of Dev C++ and msvc would tip you off to the fact that i am using c/c++. and since you have anwered it many times before, would you be so kinda s to provide a link to that post because when i search for your posts, all i get is remarks such as the one you left on my thread.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Vber
    Works fine, so I think the problem is that you're getting a wrong value from the file...
    No, it shouldn't work.

    You can not create an array that has its size based on a variable!!! You have to dynamically allocate memory for it instead.

    When the program is compiled, it has to know how big the array is or else it wouldn't be able to know how much of the stack to take up. If your array's size is based on a variable, this can not be calculated at compile time, so your code shouldn't even compile on a working compiler!

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Originally posted by Polymorphic OOP
    No, it shouldn't work.

    You can not create an array that has its size based on a variable!!! You have to dynamically allocate memory for it instead.

    When the program is compiled, it has to know how big the array is or else it wouldn't be able to know how much of the stack to take up. If your array's size is based on a variable, this can not be calculated at compile time, so your code shouldn't even compile on a working compiler!
    Yeah I know, and that's why I posted too...
    It's strange, but it compiles and compiles well... and another thing, in a working compiler (gcc) he won't compile the code, I declared the variable after starting the program (not in the start of a block or even the start of a function).

  7. #7
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    so how do you think i should go about getting this done? is there a possible way? how do i allocate memory for a two dimensional array, is it just the number of elements times some number or something like that or is there a specific way to allocate memory for such an array?
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>how do i allocate memory for a two dimensional array
    http://www.cprogramming.com/cboard/s...threadid=17742


    >>You can not create an array that has its size based on a variable!!! You have to dynamically allocate memory for it instead.
    Unless you've a C99 compiler.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    4
    "Yeah I know, and that's why I posted too...
    It's strange, but it compiles and compiles well... and another thing, in a working compiler (gcc) he won't compile the code, I declared the variable after starting the program (not in the start of a block or even the start of a function)."

    With gcc, to get C99 code to work you can use the std option.

    gcc code.c -ocode -std=c99

    that should allow any supported c99 code to work.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    C99 supports variable-length arrays. An example from the standard:

    Code:
    #include <stddef.h>
    
    size_t fsize3(int n)
    {
        char b[n+3]; // variable length array
        return sizeof b; // execution time sizeof
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. struct packet array sizes?
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 12-09-2008, 04:55 AM
  3. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  4. Joining array elements and save in variable.
    By Nutshell in forum C Programming
    Replies: 12
    Last Post: 01-12-2002, 01:06 AM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM