Weird problem with passing variables to array

Status
Not open for further replies.

Epyon

Well-known member
Okay, I've got this weird problem with passing a pointer and an integer to a function. I have a char array containing a filename and I want to edit this filename every other day. The filename also contains an ID which has to be changed. This is my code.

Code:
void getFilename(char *filename,  int id) {
    int yeari = year(); int monthi = month(); int dayi = day();
    char yBuf[4];
    itoa(yeari, yBuf, 10);
    filename[0] = yBuf[0];
    filename[1] = yBuf[1];
    filename[2] = yBuf[2];
    filename[3] = yBuf[3];
    filename[4] = monthi/10 + '0';
    filename[5] = monthi%10 + '0';
    filename[6] = dayi/10 + '0';
    filename[7] = dayi%10 + '0';
    filename[8] = '_';
    filename[9] = id%10 + '0';
    filename[10] = id/10 + '0';
    filename[11] = '.';
    filename[12] = 'c';
    filename[13] = 's';
    filename[14] = 'v';
    return;
  }
When I serial print the resulting filename, it gives me gibberish. When I don't edit positions 9 and 10, everything works fine. If I declare an integer 'id' with a value in the function, everything works fine too. Using itoa on id doesn't work.

Am I missing something on passing variables to functions here?
 
Last edited:
Okay, I've got this weird problem with passing a pointer and an integer to a function. I have a char array containing a filename and I want to edit this filename every other day. The filename also contains an ID which has to be changed. This is my code.

Code:
void getFilename(char *filename,  int id) {
    int yeari = year(); int monthi = month(); int dayi = day();
    char yBuf[4];
    itoa(yeari, yBuf, 10);
    filename[0] = yBuf[0];
    filename[1] = yBuf[1];
    filename[2] = yBuf[2];
    filename[3] = yBuf[3];
    filename[4] = monthi/10 + '0';
    filename[5] = monthi%10 + '0';
    filename[6] = dayi/10 + '0';
    filename[7] = dayi%10 + '0';
    filename[8] = '_';
    filename[9] = id%10; //+ '0';
    filename[10] = id/10;// + '0';
    filename[11] = '.';
    filename[12] = 'c';
    filename[13] = 's';
    filename[14] = 'v';
    return;
  }
When I serial print the resulting filename, it gives me gibberish. When I don't edit positions 9 and 10, everything works fine. If I declare an integer 'id' with a value in the function, everything works fine too. Using itoa on id doesn't work.

Am I missing something on passing variables to functions here?

Forum rule!
can you make a small sketch that demonstrates this issue.
as long 'id' is between 0 and 99 it should work apart from fact that you forgot '+'0' to have a readable filename
 
Well the code isn't that much more elaborate :p .

Code:
#include <TimeLib.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SPI.h>

SdFat SD;
File myFile;

unsigned long utc = 1457709481; //just for testing
char filename[] = "00000000000.CSV";
int prevDay, ID;

void setup() {
  delay(3000);
  Serial.begin(9600);
  Serial.println("starting");
  setTime(utc);
  Serial.println("Setting my internal clock");
  setTime(utc);

  // initialise the SD card
  digitalWrite(10,HIGH);
  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }
}

void loop() {
  if(prevDay != day()){
    Serial.println(filename);
    getFilename(filename, 11);
    Serial.println(filename); 
    myFile = SD.open(filename, FILE_WRITE);
      if (myFile) {
        Serial.print("Writing to file...");
        myFile.println("testing 1, 2, 3.");
        // close the file:
        myFile.close();
        Serial.println("done.");
      } 
      else {
        // if the file didn't open, print an error:
        Serial.println("error opening file");
      }
   prevDay = day();
  }
}

void getFilename(char *filename,  int id) {
    int yeari = year(); int monthi = month(); int dayi = day();
    char yBuf[4];
    itoa(yeari, yBuf, 10);
    filename[0] = yBuf[0];
    filename[1] = yBuf[1];
    filename[2] = yBuf[2];
    filename[3] = yBuf[3];
    filename[4] = monthi/10 + '0';
    filename[5] = monthi%10 + '0';
    filename[6] = dayi/10 + '0';
    filename[7] = dayi%10 + '0';
    filename[8] = '_';
    filename[9] = id%10 + '0';
    filename[10] = id/10 + '0';
    filename[11] = '.';
    filename[12] = 'c';
    filename[13] = 's';
    filename[14] = 'v';
    return;
  }

This gives me the serial output:
Code:
starting
Setting my internal clock
00000000000.CSV
Z¸e+víÚa
error opening file

Commenting out array positions 9 and 10 successfully gives:
Code:
starting
Setting my internal clock
00000000000.CSV
20160311_00.csv
Writing to file...done.
 
Last edited:
I'd review the size of yBuf[] and the itoa() documentation. Remember, char * strings have a terminating null.
 
Increasing yBuff[] with one byte indeed solved the problem. Weird, because I thought by just using the first four bytes I didn't have to worry about the terminating null. :rolleyes:
 
Itoa isn't overflow protected. So if you want a 4 digit character it will place the '/0' in an undefined space in memory. My experience of this is it can cause some pretty weird effects
Or perhaps it's placing the '/0' at the end. I'm not sure.. perhaps the teensy has a custom itoa
 
Consider something like:

itoa((int)year(), &filename[0], 10); // replaces 7 lines of code
itoa((int)month(),&filename[4], 10);
 
Last edited:
Status
Not open for further replies.
Back
Top