Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 28-01-2009
Member
 
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
Reply With Quote
  #2  
Old 28-01-2009
Member
 
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
Reply With Quote
  #3  
Old 28-01-2009
Member
 
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'
Reply With Quote
  #4  
Old 28-01-2009
Member
 
Join Date: Mar 2008
Posts: 232
Re: How to define Array size dynamically

Quote:
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 + +
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 07:16 AM.