C: Define global array variable that user can declare its size -


i want set global reference of int array, in c language, want initialize inside main function (actually user going declare size). knows how done?

thanks in advance!

declare pointer int global variable , initialize in main using malloc.

/* outside function, it's global variable: */ int *array; size_t array_size;  /* inside main(): */ array_size = user_defined_size; array = malloc( sizeof(int)*array_size); if ( array == null) {     /* exit - memory allocation failed. */ } /* stuff array */ free(array); 

if need access global variable module (source file), declare there again using

extern int *array; extern size_t array_size; 

or, preferably, declare them extern in header file included in source file uses array, including source defined (to ensure type consistency).


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -