Creating an arbitrary file name into SD card through serial port

Dear Arduino Community!

I would like to create an arbitrary txt file for my sensor readings into my SD card.
My concept: Arduino requires a file name in the void setup. When a valid file name is typed, then go to the void loop part.
My problem is that the my createFileName function does not store the typed characters after callig, however inside the function storing well.

#include <string.h>
#include <SD.h>
#include <SPI.h>

char *myFileName;
char *extension =".txt";
File myFile;
  
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // Initiate SD Card data logger
  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);   // make sure that the default chip select pin is set to output, even if you don't use it

  // See if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println("Card failed, or not present.");
    return;     // don't do anything more
  }
  Serial.println("Card initialized.\n");
    
  // Create a file name
  Serial.println("Type a valid file name for storing the raw measurements!");
  Serial.println("You must only add the 8 or less character of the 8.3 format.");
  Serial.println("The file names look like 'NAME001.TXT', where 'NAME001' is an 8");
  Serial.println("character or fewer string, and 'TXT' is the given 3 character extension.");
  myFileName = createFileName();
  strcat(myFileName,extension);

  // open a new file and immediately close it:
  Serial.println("Creating your typed file name...");
  myFile = SD.open(myFileName, FILE_WRITE);
  myFile.close();


  if (SD.exists(myFileName)){
    Serial.println("Your typed file name exists.");
  }
  else {
    Serial.println("Your typed file name doesn't exist.");
  }

  Serial.println(myFileName);

}

void loop() {
//Don't do anything.
}

char *createFileName(){
  char typedFileName[8] = "a";
  int ix = 0; // Character counter
  int ix1 = 0; // Loop turn off
  while (ix1 == 0) {
    while ( Serial.available() > 0 ) {
      char recieved = Serial.read();
      if (ix < 8) {
        typedFileName[ix] = recieved;
      }
      ix++;
      delay(25); // Need for stability.
    }
    if (ix > 8) {
      Serial.println("Type at most 8 character!");
      ix = 0; // Reset the character counter.
    }
    else if (ix != 0 ) {
      typedFileName[ix] = '\0';
      ix1 = 1; // Exit of the 1st while loop.
      Serial.println(typedFileName);
      return(typedFileName);
    }
  }
}

Any suggestions or help you guys can provide would be very much appreciated.

Thank so much.

Rather than returning a pointer and dealing with scope and possible memory leaks here's a working version of your code using global character strings.

#include <string.h>
#include <SD.h>
#include <SPI.h>

//char *myFileName;
char myFileName[13]; //8.3 + \0 
char typedFileName[9]; //Serial entry 8 characters plus \0
char *extension =".txt";

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // Initiate SD Card data logger
  Serial.println("Initializing SD card...");
  pinMode(10, OUTPUT);   // make sure that the default chip select pin is set to output, even if you don't use it

  // See if the card is present and can be initialized:
  if (!SD.begin(4)) {
    Serial.println("Card failed, or not present.");
    return;     // don't do anything more
  }
  Serial.println("Card initialized.\n");

  // Create a file name
  Serial.println("Type a valid file name for storing the raw measurements!");
  Serial.println("You must only add the 8 or less character of the 8.3 format.");
  Serial.println("The file names look like 'NAME001.TXT', where 'NAME001' is an 8");
  Serial.println("character or fewer string, and 'TXT' is the given 3 character extension.");
  //myFileName = createFileName();
  createFileName(); //takes serial entry into typedFileName
  //strcat(myFileName,extension);
  strcpy(myFileName, typedFileName);
  strcat(myFileName, extension);
  Serial.println(myFileName);
  // open a new file and immediately close it:
  Serial.println("Creating your typed file name...");
  myFile = SD.open(myFileName, FILE_WRITE);
  myFile.close();


  if (SD.exists(myFileName)) {
    Serial.println("Your typed file name exists.");
  }
  else {
    Serial.println("Your typed file name doesn't exist.");
  }

  Serial.println(myFileName);

}

void loop() {
  //Don't do anything.
}

//char *createFileName(){
void createFileName() {
  //char typedFileName[8] = "a";
  int ix = 0; // Character counter
  int ix1 = 0; // Loop turn off
  while (ix1 == 0) {
    while ( Serial.available() > 0 ) {
      char recieved = Serial.read();
      if (ix < 8) {
        typedFileName[ix] = recieved;
      }
      ix++;
      delay(25); // Need for stability.
    }
    if (ix > 8) {
      Serial.println("Type at most 8 character!");
      ix = 0; // Reset the character counter.
    }
    else if (ix != 0 ) {
      typedFileName[ix] = '\0';
      ix1 = 1; // Exit of the 1st while loop.
      Serial.println(typedFileName);
      //return(typedFileName);
    }
  }
}