If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > global arrays in C

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-13-02, 13:44
somealien somealien is offline
Registered User
 
Join Date: May 2002
Location: MA
Posts: 4
Angry global arrays in C

I'm trying to create an uninitialized global array in C ... does anyone know if i can do this, even? i want the user to enter the dimentions of the array in the main method so i can't just say float array[user_input]; because user_input is not yet given a value.

does anyone know how to do this (preferably without pointers)?

thanks a lot
Reply With Quote
  #2 (permalink)  
Old 06-18-02, 05:00
Apel Apel is offline
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 228
Well, arrays in c are not really much more than a pointer to the first element of it. There is no bounds checking at all. So using arrays is actually working with pointers. You can use malloc to allocate memory for a variable size array.

Code:
unsigned int array_size;
A_NICE_TYPE *myarray;

/*...*/

scanf("%u",&array_size);
myarray = malloc(sizeof(A_NICE_TYPE)*array_size);
if(myarray == NULL)
{
    /*out of mem*/
}

/*have fun with your array*/

free(myarray);
You can access the array just like every normal array.
myarray[0] does the same as *myarray
and don't forget the free
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On