Pages: [1]   Go Down
Author Topic: Passing char* to Serial.println  (Read 266 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i'm trying to do my first project with arduino. So far i've done an accelerometer logger with an interruption to put the arduino to sleep. Now i want to make it use a new file to log the data every time the interruption is called so I've a counter to know the current file number and a function to return the filename where to store the data on the SD.

So I've this function to generate the string:

Code:
char* createFilename(int number)
 {
   // Per tracejar l'exponent
   int counter = 0;
   
   // Per guardar el resultant de la divisió
   int rest;
   
   // Per emmagatzemar els diversos caracters a convertir
   char values[100] = "";
   
   // Per reordenr la cadena a tornar
   char output[100] = "";
   
   // Per emmagatzemar el resultat de la divisió
   int result = number;
   
   while(result>9)
   {
     rest = result%10;
     result = result/10;
     values[counter] = '0'+rest;
     counter++;
   }
   values[counter] = '0'+result;

   int j = counter;

   for(int i=0;i<=counter;i++)
   {
     output[i] = values[j]; 
     j--;
   }
   output[counter+1]='.';
   output[counter+2]='C';
   output[counter+3]='S';
   output[counter+4]='V';
   output[counter+5]='\0';
   return output;
 }

I've a do a Serial.println(output) here the output is the desired one, for example: 1.CSV

But when I call this function from the loop I get random characters instead the desired output.

Code:
Serial.println(createFilename(availableFile));

I suspect that I'm doing something wrong with the pointers.

Thank you.
Logged

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
your output is declared inside createFilename(), so when the function returns you cannot rely on it (it goes out of scope). The memory taken by your array within the function can be reused by the system at any time, so the pointer returned by createFilename() may point to totally different data. One of the things you can do is pass the char array as an argument to the function, as in:

Code:
char* createFilename(char* prefix, int number) { ... }

You may also consider sprintf().
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you spatula! Worked like a charm smiley-wink

Trying to remember C, used long time ago..., the variable would have been preserved in memory if I used malloc to allocate the memory?
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you spatula! Worked like a charm smiley-wink

Trying to remember C, used long time ago..., the variable would have been preserved in memory if I used malloc to allocate the memory?

Yes, if you free it after you print it.

However, using malloc is often a bad idea and certainly something to avoid on a microcontroller, especially if you are allocating 5% of the memory at a time (100 bytes out of a possible 2048)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. You're right, I only used C on Computers where memory is cheap  smiley-wink
Logged

Pages: [1]   Go Up
Jump to: