Thread: Basic Array Initialization in C

  1. #1
    Unregistered
    Guest

    Basic Array Initialization in C

    Could someone please tell me how I can initialize an array to 1 please? The array has a size of 101 and the only way I can get it to set to 1 is to type:

    #define ARRAY_SIZE 101
    #define TRUE 1
    #define FALSE 0

    int array [ARRAY_SIZE] = {1,1,1,1,1...},

    with 101 1's!! What's the shorthand way of achieving this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    #define T_5     TRUE,TRUE,TRUE,TRUE,TRUE
    #define T_25    T_5,T_5,T_5,T_5,T_5
    int array [ARRAY_SIZE] = {T_25,T_25,T_25,T_25,TRUE};

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    use a for loop (it's not technically initialisation) but it's more elegant than that^.

  4. #4
    Unregistered
    Guest
    Use memset();


    int array[10];

    memset(array,1,10);


    you have to use the string.h library, but it is a lot cleaner than the for loop or one at a time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM