How to reset the values in a char pointer array

Hello, in my code I need to reset a char pointer array while the Arduino is running (ex. char id[50]; ), after I fill it with data.
I can't wait to reset it or relaunch it. I've tried the various methods listed below but none of them would work.

I tried memset

memset(id, '\0', 50);

but that didn't work.

I tried creating another char[]; and copying its blank contents into the original char

char idc[50];
char id[50]; //is filled with data
memcpy(id, idc, 50);

but that also didn't work.

I even tried the simple:

char idc[50];
char id[50]; //is filled with data

id = idc

But of course that wouldn't work (I was desperate).

So I must ask does anyone know how to reset a char pointer array while the Arduino is running without having to reset it?

Thanks, any help would be greatly appreciated!

You can use a for loop,

for ( uint8_t i = 0; i < 50; i++ )
  id[i] = 0;

But, memset should work :S

If you're really using the array as a C string, you can simply set the first element to zero.

But, as guix says, memset should have worked.

-br

Could you make a small test sketch and paste it in a post, or you could post your whole sketch.
The code between [code] ... [/code] tags please.

memset() works, I use it myself.

My code is pretty long >130 lines but the problem is the first statement in void loop() so it's pretty easy to find.
I really appreciate any help!
The code is pretty fun to use, just need an Arduino with SD card and SD card shield.
Run the code and open the Serial monitor. Then type in an 8 digit number ID (random numbers will work) and then type in an amount of money, (numbers and terminate the numbers with an 'e', like this --> 123e). It'll create the file and print some stuff. And then it'll ask for another ID again, the problem is when you run through it again it doesn't work, I need the id[] and money[] arrays to be cleared because the code doesn't work with the old values stored there.
I hope this makes sense, feel free to run this code, it's really cool!

#include <SD.h>
#include <string.h>
byte SD_CS = 4; //Chip select of SD reader
File cust_file; //to call text file 
char id[50]; //name of text file
int id_index = 0;
char money[50]; //value in text file
int money_index = 0;
char money_read[50]; //value read from text file
int money_read_index = 0;
float valueFloat; //typecasted valueReceive
int input; //user input
byte id_counter = 0;
byte money_counter = 0;
float old_money, current_money, new_money; //old is stored, current is entered, new is to write
char cl = '\0';
boolean clrd = 0; //only clears memory when data has already been written

void setup(){
  Serial.begin(9600); //open serial port
  Serial.println("Initializing SD Card");
  pinMode(SD_CS, OUTPUT); //CS for SD card 
  pinMode(10, OUTPUT); //CS must be an output for SPI to work
  	   
  if(!SD.begin(SD_CS)){ //tests to see if SD card initialization failed
  Serial.println("SD card initialization failed");
  return;
  }
  Serial.println("SD card initialization success");
  	  
  Serial.println("Enter in ID:"); //prompts user to enter in a value to store
  id[id_index++] = '/';  // Root directory
}

void loop(){
  while(clrd){ //part that creates the problem!
    memset(id, '\0', 50);
    memset(money, '\0', 50);
    clrd = 0;
  }	      
  get_id();
  get_current();
  get_old();
  get_new();
}

void get_id(){ //gets ID(filename) from user to name file
 
  while(Serial.available()){ //read data from Serial port
    input = Serial.read();
    if(input>='0' && input<='9'){
      id[id_index++] = input;
    }
    strcpy(id+id_index, ".txt"); //converts to pointer array
    id_counter++;
  }
  
  if(id_counter>=8){ //if filename complete
    Serial.print("ID Entered is: "); //print id entered to Serial
    Serial.println(id);
    Serial.println("Enter in money:"); //prompt user to enter in money
  }
  else{
   get_id(); //otherwise go back to function
  }
}

void get_current(){ //gets current input from user
  id_counter = 0; //prevents code from repeating itself over and over.
  while(Serial.available()){ //reads data from serial port and save in pointer array
    input = Serial.read();
    if(input>='0' && input<='9'){
      money[money_index++] = input; //saves user input to money index
    }
    else if(input=='e'){
     money_counter = 1; 
    }
  }
  if(money_counter==1){
    strcpy(money+money_index, ""); //converts to pointer array
    current_money = atof(money); //current_money is saved from money pointer array after being converted to float
    Serial.print("Money entered is: "); //print money entered to Serial
    Serial.println(current_money);
    Serial.println(" "); //blank line
  }
  else{
   get_current(); 
  }
}

void get_old(){ //gets old value from file if exists, if file doesn't exist create one first
  money_counter = 0; //prevents code from repeating itself over and over.
cust_file = SD.open(id, FILE_READ); //open file at beginning
  if(!cust_file){ //if file not already present, create one
    cust_file = SD.open(id, FILE_WRITE); //create file 
    Serial.print("Created file: "); //prints new file name
    Serial.println(id);
    Serial.println(" ");
  }
  if(cust_file){ //if file is available
	  while(cust_file.available()){
            for(byte c=0; c<50; c++){
	      money_read[money_read_index++] = cust_file.read(); //store file contents 
	    }
	  }
  old_money = atof(money_read); //stores the array as a float in old_money
  Serial.print("File available, old money stored is: "); //prints old money stored 
  Serial.print(old_money);												                //in file to Serial		Serial.println(old_money); //print old_money
  Serial.println(" "); //blank line
  cust_file.close(); //close file 
  }
}

void get_new(){ //get new_money by computing current_money and old_money
  new_money = current_money + old_money;
  if(new_money>=100.0){ //if new_money greater than 100 than print coupon and subtract 100
    new_money -= 100.0; 
    Serial.println("You have a $5 discount!");
    Serial.print("You have $");
    Serial.print(new_money);
    Serial.println(" left.");
    Serial.println(" ");
  }
  else if(new_money<100.0){ //if new_money less than 100 print new_money
    Serial.print("You have $");
    Serial.print(new_money);
    Serial.println(" now.");
    Serial.println(" ");
  }
  cust_file = SD.open(id, FILE_WRITE);
  cust_file.print(new_money);
  cust_file.close();
  Serial.println("File written."); //shows user that file has been successfully written						  
  Serial.println(" "); //prints blank line
  Serial.println("Enter ID:"); //prompts user to enter next ID
  clrd = 1; //lets the data in id and money arrays clear
  loop();
}

This is a very creative way to terminate a char array:

   strcpy(money+money_index, ""); //converts to pointer array

But this would work the same and generate less object code:

   money[money_index] = 0;

Could you state your problem description more clearly, please? What are you trying to accomplish, and what seems to be going wrong?

-br

Yeah I edited my previous post it explains what the code is trying to accomplish.

the problem is when you run through it again it doesn't work

What does "it doesn't work" mean? What does it do that you don't expect, or not do that you do expect, when you clear the arrays using memset or setting the zeroeth element to zero?

"It doesn't work" is the world's worst bug report, because it tells us nothing.

-br

I apologize, what I mean is that it won't print out the new ID to the Serial monitor and after you give it a new money value it won't run the rest of the code to save the file, like it does the first time you run it.

it won't run the rest of the code to save the file, like it does the first time you run it

So, what happens instead?

You need to go on the attack and instrument your program to tell you where it's screwing up. Sprinkle Serial.print() statements near where you think it's leaving the tracks. Log a word to say where you are, or an important value. Soon you will find some output that is not what you expect, something wrong the second time. It will lead you to your bug.

It may not be doing the right thing the second time around, but it is doing something. Figure out what that wrong thing is, and you'll be on the way to making it work.

Good luck with your project.

-br

PS: It wouldn't hurt to re-terminate your input arrays every time you add a a character to them.

Thank you, I will try that now. It's the only logical thing left. I really appreciate your help everyone, especially to billroy for sticking with me on this one!

A standard pattern which works very well is:

            if(length < BUFF_SIZE)
            {
                buffer[length++] = c; // append the received character to the array
                buffer[length] = 0; // append the null terminator
            }
            else
            {
                // buffer full - discard the received character
            }

Remember to make the array big enough to include the null terminator. I prefer to do this by declaring it as size BUFF_SIZE+1 so that I can use BUFF_SIZE as the number of characters it can hold not counting the terminator.