Your immediate questions:
- Is it correct to declare a string with uint16_t?
No.
All strings are always char[]
. There ar also wide strings (strings of wide characters), which have the type wchar_t[]
, and are written with an L
prefix (e.g. L"hello"
).
- What is recommended way to pass a string to a function?
As a pointer to the first character in the string, so either char *
or wchar_t *
. The const
qualifier makes no sense here; it would mean that the pointer (not the string itself) is constant (see here). I'd recommend writing wchar_t const *
or const wchar_t *
instead; it has a different meaning (i.e. the string can't be changed), but it's correct and meaningful.
You mention that this code is from a course book. I'm not sure whether you mean that these are lecture notes handed out by your professor, or whether you bought a published book. In either case, if this snippet is representative of the quality of the book/notes, get a refund.
For now, let's make this code work.
There's an error in this code that will cause the program to crash:
printf("... %s ...", ..., *string, ...)
when string
is a char*
is always wrong. Don't dereference that pointer.
If you insist on using "wide" characters (which is questionable), you're going to have to change a few things:
- Rather than using
char
, you need to include <wchar.h>
and use wchar_t
instead. As far as I know, uint16_t
and uint8_t
won't work unless you use explicit casting everywhere. That's because char
doesn't have 8 bits, but CHAR_BIT
bits.
- Wide character literals must start with an
L
prefix.
- Rather than using
printf
, you need to use wprintf
.
- In the format string, use
%ls
rather than %s
. (Unless you use Microsoft's compiler.)
Finally, some less grave errors:
- As noted,
T * const
arguments are useless. The author probably meant T const *
(or equivalently const T *
).
- You can remove
<stdint.h>
and <stdio.h>
. We're no longer using uint16_t
, and <wchar.h>
declares some wide character <stdio.h>
-like functions.
I end up with the following code:
#include <wchar.h>
void Terminal_PrintData(wchar_t const * Data);
int main(void){
wchar_t StringData[] = L"MyData";
Terminal_PrintData(StringData);
}
void Terminal_PrintData(wchar_t const * Data)
{
wprintf(L"Value: %ls", Data);
}
This compiles and runs as expected with both GCC and Clang on Linux x86-64.