Results 1 to 4 of 4

Thread: How to define Array size dynamically

  1. #1
    Join Date
    Jan 2009
    Posts
    92

    How to define Array size dynamically

    Hi guys!

    I'm taking over the C language after more than a year, and I suddenly collapsed a certainty
    As far as I know, when you define an array of its size should be known at compile time, and in particular it should be a constant. Now, doing some evidence, I realize that compiling and running a program like this:

    Code:
      # include <stdio.h> 
    
      int main (void) 
      ( 
         int c; 
         int DIM; 
         printf ( "Enter array size:"); 
         scanf ( "% d", & DIM); 
         int string [SIZE]; 
         for (c = 0; c <DIM, c + +) 
           string [c] = c; 
         printf ( "\ nFine \ n"); 
         return 0; 
      )
    not only do not get errors in compiling, but not even a segmentation fault at runtime.

    If you could explain to me why?

    Accurate use gcc version 4.2.4 on Linux 2.6.24 64-bit

  2. #2
    Join Date
    Oct 2008
    Posts
    167

    Re: How to define Array size dynamically

    the C99 can be done, is the c + + which does not support

  3. #3
    Join Date
    Jan 2009
    Posts
    92

    Re: How to define Array size dynamically

    I tried to fill this with g + +, and not even get a runtime error ...

    Code:
     # include <iostream> 
    
      using namespace std; 
    
      int main (void) 
      ( 
         int d; 
       
         court << "Enter array size:"; 
         cin>> d; 
       
         int carrier [d]; 
       
         for (int i = 0; i <d-1, i + +) 
           vector [i] = i; 
       
         court << "Done!" <<endl; 
       
         return 0; 
      )
    EDIT: Only if you fill a passing the-pedantic I get the warning on variable size of the array, but not a mistake ...

    Code:
    warning: ISO C90 forbids variable-size array 'string'

  4. #4
    Join Date
    Mar 2008
    Posts
    232

    Re: How to define Array size dynamically

    I tried to fill this with g + +, and not even get a runtime error ...


    Code:
    # include <iostream> 
    
      using namespace std; 
    
      int main (void) 
      ( 
         int d; 
       
         court << "Enter array size:"; 
         cin>> d; 
       
         int carrier [d]; 
       
         for (int i = 0; i <d-1, i + +) 
           vector [i] = i; 
       
         court << "Done!" <<endl; 
       
         return 0; 
      )
    EDIT: Only if you fill a passing the-pedantic I get the warning on variable size of the array, but not a mistake ...
    precisely because it prohibits not the C90 and C99 as the gcc compiler C99 is a string that is accepted, and its validity "imported" in c + +

Similar Threads

  1. Dynamically allocate a two-dimensional array
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 24-10-2009, 11:43 AM
  2. Size of a pointer to an array of char
    By Zool in forum Software Development
    Replies: 3
    Last Post: 14-10-2009, 12:05 PM
  3. How to return a dynamically allocated char array?
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 09-10-2009, 01:47 PM
  4. How can I define font size in PHP page?
    By RedIndigo in forum Software Development
    Replies: 3
    Last Post: 10-07-2009, 11:40 PM
  5. What stripe size should I use in Raid 5 array
    By jade in forum Hardware Peripherals
    Replies: 3
    Last Post: 19-06-2009, 09:12 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,856,113.35828 seconds with 16 queries