Char, string, sprintf, what is the same and different about them?

Does anyone have a link to a page that describes well the use of variables and data types in arduino code?

Yes I know arduino.cc has links to Data Types, but it doesn't go in dept on what each can and can't do.

I am asking this because I am having a very hard time getting the right one to work. I have been swapping between char, word, floats, string, arrays, and sprintf's. They all act different, and don't seam to work well together.

To me it seams they can all do the same thing, yet no they can't, except when they want to, but only if they are suppose to.

For example. sprintf(templog_filename, "tp%02d%02d%02d.txt", year, month, monthDay ); builds a tp161001.txt string.

But later when I want to use that string, say to make

char file_name = templog_filename. I get errors

But if I define "char file_name[13];" at the begining of the code, I get different errors.

I only got it to work when I wrote the following:

sprintf(file_name, templog_filename);

Yet in other parts of my code. I have a byte "byte LEDpowerStatus = 0;" that I can easily change to another varible using:

void LED_power_loop(byte StatusChange) { /// called from LEDpowerStatus(1);
LEDpowerStatus = StatusChange;
}

Why does that work, yet the other does not.


For the second part of my code, I wanted to define "file_folder' but all the following fails and create errors.

file_folder = ("cwd temperature");
file_folder = "cwd temperature";

It is going to change depending on what director needs to be accessed. So I tried to define it in the beginning, but all the following fail:

char file_folder;
char file_folder;
char file_folder[16];


My various arduino projects are web page, and readable log files. And I need to use characters (ABCDEFGHI...) and sometimes with numbers, that have been combined together. My code is all over the place because CHAR works sometimes in certain areas, if not, I need to use sprintf's that I can't convert.

Can anyone lend some light on better explaining what everything does. Arduino.cc only sorta tells you what something is. Not what it can and can't do. And google results are all over the place and usually to one specific line of code.

http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr

And sprintf() is a function that works with char arrays, not a data type itself.

The key to keep in mind is that a char only holds one character. To get a string you need an array of them and assignment to an array works differently from assignment of a simple variable. When you learn to think of strings as arrays it all gets clearer.

My terminology is probably wrong but you can only assign a value in the way you do when you declare ithe variable.

char filefolder[16];

Create a variable that can hold 15 characters and the terminating nul character.

char filefolder[16] = "some_folder";

Create a variable that can hold 15 characters and the terminating nul character and initialise it with the given string.

If you want to assign a value later in your code, you can make use of sprintf() or strcpy()

strcpy(filefolder, "some_folder");

The solution for the filename could be the use of a pointer

char *filename;
sprintf(templog_filename, "tp%02d%02d%02d.txt", year, month, monthDay );
filename = templog_filename;

Please note that filename will reflect any changes that you make to templog_filename; if you want to prevent that, you need to make a copy (and can't use a pointer).

if you want to prevent that, you need to make a copy (and can't use a pointer).

Why can't you?

char someCrap[] = "This is some crap";

char *pointerToCopyOfCrap = strdup(someCrap);

Now, pointerToCopyOfCrap points to a copy of someCrap.

So many good answers. ...

KeithRB:
http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=aryptr

And sprintf() is a function that works with char arrays, not a data type itself.

Trying to find info on how to use a sprintf correctly is part of my problem. My history dates back to writing up scripts for mIRC which uses a baster'idezed C+. And in their case, a variable is the same as an interger, is the same as a temporary. I had to use a sprintf to build a string, but then still be able to use that variable else where in the code. Would you mind explaining a bit more about how it is a part of char array, and not just an array string on its own?


Delta_G:
The key to keep in mind is that a char only holds one character. To get a string you need an array of them and assignment to an array works differently from assignment of a simple variable. When you learn to think of strings as arrays it all gets clearer.

That's what I thought when at the beginning, I would create the "char file_name[18];" at the start of the the code.
This way, there would be 18 spaces alottled for this char.


sterretje:
My terminology is probably wrong but you can only assign a value in the way you do when you declare ithe variable.

char filefolder[16];

char filefolder[16] = "some_folder";

I have done both, but it still will give me an error that the filefolder string is not defined. Or when it SerialPrint me aline, I get jibberish. Currently it's showing this character: Ÿ

Also, I will have various folders involved, all with a different lenght. temperature (11), mailbox (8), modlog (6).

If you want to assign a value later in your code, you can make use of sprintf() or strcpy()

strcpy(filefolder, "some_folder");

I will make an attempt to use strcpy, and see if it doesn't yell at me. But don't I still have to create a char filefolder[18]; ?

EDIT: Tried both strcpy, and sprintf. both crash the arduio, and it reboots.

The solution for the filename could be the use of a pointer

char *filename;

sprintf(templog_filename, "tp%02d%02d%02d.txt", year, month, monthDay );
filename = templog_filename;



*Please note that filename will reflect any changes that you make to templog_filename; if you want to prevent that, you need to make a copy (and can't use a pointer).*

can you explain why there is a * in the char *filename; line?


can you explain why there is a * in the char *filename; line?

char * is a pointer to a (or an array of) char(s).

But don’t I still have to create a char filefolder[18]; ?

Yes. You need some place to copy the data to.

I have done both, but it still will give me an error that the filefolder string is not defined. Or when it SerialPrint me aline, I get jibberish. Currently it’s showing this character: Ÿ

No handwaving. Create a simple sketch that illustrates the problem. Then, we can help you understand what the problem is, or why the code is behaving the way that it is.

See paste bin link for full code:

INCLUDE-ETHERNET:
This is where I am trying to define the char.

SETUP

LOOP
Typing T in the serial window, will trigger the startFTP void that is located below

SERVER - FTP UPLOAD - STEP 2

This is where I am having the issue. Step 2 , if the command 'templog_action" was sent, then
Serial Print a line,
Change file_name to tx161001.txt (templog_filename is defined below in the RTC section)
Change file_folder to "cwd temperature" // so it will change the directory.

Then execute the startFTP upload if statment " if (startFTP_ready == 1) {" Which triggers the file upload.

As you will see in the bottom of the Step 2 section. there is serial.prints, that will echo what I see. This is where the failure starts. (The FTP upload means nothing until this gets fixed)

RTC - Real Time Clock Get Time

This is where the templog_filename sprintf is created

This is where I am trying to define the char.

You are going to make a lot more progress when you understand that a char holds ONE character and that a char array can hold more than one, and when you use the proper terms.

This is where the templog_filename sprintf is created

You are NOT creating a sprintf. You are using the sprintf() function to populate an array called templog_filename.

At least I suppose you are. I'm not going to dig in the rubbish bin to find your code. Post it here. The stickies at the top of the forum explain how, and you were supposed to read those FIRST.

Typing T in the serial window, will trigger the startFTP void that is located below

Sorry, but calling a function a "void" tells me that you have little grasp of the C (or any) computer language.

PaulS:
You are going to make a lot more progress when you understand that a char holds ONE character and that a char array can hold more than one, and when you use the proper terms.
You are NOT creating a sprintf. You are using the sprintf() function to populate an array called templog_filename.

Ok, that makes sense. Could I just use a String templog_filename ( ) to build it also? I had to use a sprintf() in the past as it was suggested from another forum post. I am just looking for clarification.

At least I suppose you are. I'm not going to dig in the rubbish bin to find your code. Post it here. The stickies at the top of the forum explain how, and you were supposed to read those FIRST.

I could not post my code here due to the 9000 character limit. And posting just snippits of the area's of concern would (as complained about in other posts) not allow other people to compile and test their theorys.
The full code is located here: http://pastebin.com/yx0UVUjr


outsider:
Sorry, but calling a function a "void" tells me that you have little grasp of the C (or any) computer language.

As mentioned before, My codeing skills derived from programming for mIRC clients. And it's code is a red-headed step child of C. Sorry I am still learning the proper terminology of arduino code.

Could I just use a String templog_filename ( ) to build it also?

Yes, but there is a lot of overhead in using the String class, and memory fragmentation.

I could not post my code here due to the 9000 character limit.

The limit is 1M, if you use the Reply option and attach the code (as the stickies explain).

The full code is located here: http://pastebin.com/yx0UVUjr

In the rubbish bin. If the code is rubbish, that's the appropriate place. I, for one, will not go there to get your code.