I'm new to FB and almost new to programing. A little MS and Turbo Basic programming. I've downloaded Freebasic and want to try to write a. If you’re greeted with the SPIEL start message, then the programming worked, and you can read about the Phase 2 (next month’s) project which can access SPI chips directly, and program displays straight from the serial port. Once the programming of the 8051 type MCU has been completed, remove the three jumpers from the board to separate the.
![]()
There are major differences between Basic and C. As a starting point, I would read an online tutorial on C so you can see the syntax differences. Also, C has no intrinsic I/O functions like Basic.
They are all part of the Standard C library. If you read a tutorial, you will see how important libraries are to C, where they almost don't exist in many dialects of Basic.As to which tutorial to select, that's a matter of personal preference, plus I'm a little biased.
Google 'C tutorial' and read a little and, if you don't like what you read, try another until you find one that makes sense to you. After you complete the tutorial, chances are you won't even need to come back here with your question. The Arduino IDE and its underlying compiler, recognizes strings in the C sense, which are null-terminated character arrays. However, because the underlying compiler also supports C, you can also use the String class.
(Note the uppercase 'S'.) In most cases, I think most prefer the character array approach to using string data in C.There are numerous string processing functions available to you from the C library. Most of these begin with 'str' (e.g., strcmp, strcat, strtok, etc.) There are also a number of preprocessor macros that look like function but are actually macro, such as toupper, tolower, isalpha, etc. If you take a little time and do some Google searching, you'll likely find what you need. A redim is another animal, as there is no underlying op system to do garbage collection for you. A char holds one character.
A char array can hold more than one character, if the size is greater than one. A string is a char array that is NULL terminated. A pointer is another matter, altogether.Imagine a post office, with a row of mail boxes. You can put a letter in a box (that is a char). You can put a bunch of letters in a bunch of successive boxes (that's an array).
You can put a bunch of letters in successive boxes, with a special letter (in a red envelope, let's say) in the last box that indicates that that is the end of the bunch of letters (that's a string).A pointer is like a 3x5 card with a post office address and box number on it. You go to that address, and find that box number to get the first letter. You open the adjacent boxes to get the rest of the letters, until you get to the one with the red envelope. Then, you have the string pointed to by the pointer.
![]()
Code: int val;int.ptr;Now, take a clean sheet of paper and on the right side near the top write val. Draw a 45 degree line from the base of val down and to the right. Label the line rvalue. The term comes from assembly language days and stands for 'register value'. Now draw another 45 degree line from the base of val down and to the left and label it lvalue.
(From assembly again for 'location value'.) Let's pretend the compiler placed val at memory location 1000, so write that number at the end of your lvalue line. My Bucket Analogy likens what you've drawn to a bucket: the lvalue tells you where the bucket is located in memory, the rvalue tells you what is inside the bucket, and the int type specifier tells you that the bucket is big enough to hold 2 bytes of data. (An int on other platforms might be a 4 byte bucket.)Now on the left side of the sheet, draw the same lvalue/rvalue diagram for ptr. We'll pretend that the compiler placed this variable at memory location 1050, so write that number at the base of its lvalue. Now consider this code. Code: ptr = &val;This is also an assignment, but fashioned for a pointer assignment. The address of operator (the ampersand, &, in front of val) says that, instead of doing an rvalue-to-rvalue assignment, get the address of val (i.e., 1000) and assign it to ptr.
This means that ptr now has an rvalue that is the memory address of where the val bucket resides in memory. This leads to an important fact about pointers: A valid pointer should only store one of two things: 1) a null value, which means the pointer points to nothing useful, or 2) a valid memory address where some data are stored.Now consider the statement. A lot dependson what you want to do with the array of characters at a later stage. There are many C/C functions that are designed to work on a 'string' - an array of characters with a byte of 0 as then end marker. However if you only intend to process the data as an array of chars the end marker may be unnecessary.One thing to bear in mind is that C/C doesn't automatically extend character arrays to make more space for a longer 'string'. This is especially important on the Arduino where SRAM memory space is very limited.I suggest creating a char array long enough for your longest 'string' + 1 extra char for the null terminator.
You can easily store shorter strings - that just depends on where the terminator is.R. I have this working in an Arduino simulator.Since all these strings won't be changed, copied, compared, etc.
I defined them as constants. This also saves me SRAM memory which I need for other data structures. I had plenty of flash memory available in my program, since it uses only about 25% of code space. (Or does it; where is the text stored?)My understanding is that this definition creates a scalar array of pointers to the actual text, hence my array only has one dimension. It also allows me to refer to the text messages with a single index.One more non-critical question. Why does the simulator keep complaining about the ';' line after my constant array definition?
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |