Here is the header file (main.h) for the ATMEGA328PB (xplained board).
#ifndef MAIN_H_
#define MAIN_H_
// Serial port stuff
#include <string.h>
#include <stdio.h>
// Printing a string to the serial port
void print_serial(const char * const print_string);
// Given an input array, process the command
void go_process_command();
#endif // MAIN_H_
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Yes, there is no intention to modify the contents of the string. Just "print" it to the USART. Thanks for reviewing the code - I just posted this less than 24 hours ago! :)
Are you sure? yes | no
In a past life, I worked at a place that was fussy about the functions. If you weren't going to make changes to the contents, then the requirement was that everything was constant. I guess old habits die hard.
Are you sure? yes | no
For a pointer type declaration there are two things that can be affected by the modifier const, depending on the position of the word: the pointer, or the thing pointed to. This generalises to higher order pointers, which can lead to madness on reading. C syntax is tricky that way.
Are you sure? yes | no
Did you really intend for print_serial to have a prototype parameter of const char *const? The first const promises that your function will not alter the contents of the string pointed to which is fine, but the second const promises that your function will not alter the pointer parameter. That prevents the function from using the parameter as a local variable, making it necessary to make a copy if modification is done, e.g. advancing the pointer to find the NUL byte. Given that the function receives a copy of the pointer anyway, no purpose is served.
Here's a short program which illustrates what happens.
void print(const char *const s)
{
s++;
}
int main(void)
{
print("Hello");
}
and the compilation message:
c.c: In function ‘print’:
c.c:3:3: error: increment of read-only parameter ‘s’
s++;
^~
Are you sure? yes | no