temporary arrays

This is a discussion on temporary arrays within the C++ Programming forums, part of the General Programming Boards category; I have a temporary array that gets used when needed and then left for the next time it is needed. ...

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    temporary arrays

    I have a temporary array that gets used when needed and then left for the next time it is needed. So how can I reset the whole array without going through the long way ->
    long way
    temp_array[0]=1;
    temp_array[0]=2;
    temp_array[0]=3;
    temp_array[0]=4;
    temp_array[0]=5;

    I have tried ->
    temp_array[]={0, 0, 0, 0, 0}; and
    temp_array[5]={0, 0, 0, 0, 0};

    James
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    try this:

    //put this at top of code
    #define ArraySize(x) ((sizeof(x)) / (sizeof(x[0])))

    //declaration of array
    int *myArray = new int[5];

    ...code...

    //resetting of array
    myArray = new int[ArraySize(myArray)];

    Of course if you dont like pointers...you could always use a for loop:

    int myArray[5];
    for(int x = 0; x < 5; x++) myArray[x] = 0;
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    memset(temp_array, 0, sizeof(temp_array));
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    Code:
    int myArray[5]; 
    for(int x = 0; x < 5; x++) myArray[x] = 0;
    That's a great idea for reseting all the values to 0, I will use it one day But what about if I wish to reset the values to say {1, 2, 32, 5, 7} e.t.c
    My site to register for all my other websites!
    'Clifton Bazaar'

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by phantom
    Code:
    int myArray[5]; 
    for(int x = 0; x < 5; x++) myArray[x] = 0;
    That's a great idea for reseting all the values to 0, I will use it one day But what about if I wish to reset the values to say {1, 2, 32, 5, 7} e.t.c
    Well I guess it would depend where you are getting the "1, 2, 32, 5, 7" from......is it part of an identifiable sequence? if so, perhaps you could recreate it......if not, then you would need to hard code the resetting process

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 03:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21