Reputation: 6170
I'm on an embedded system so do not have access to a most of the standard library. I have a struct
which contains char values.
I have a simple print
function which simply outputs an unsigned char string to an attached screen. It does not support format specifiers like printf
does.
This is the struct:
typedef struct my_options{
char test;
} my_options;
And this is where I'm trying to output the value:
struct my_options options;
print(options.test); //Here I get "implicit conversion of int to ptr"
How do I achieve this?
Upvotes: 2
Views: 701
Reputation: 76415
your member test
if of the type char
, where the print
function expects an argument of the type const char *
(assuming the const
bit here, but that's what I'd expect, this as an asside). Passing the address of test
then would seem like the appropriate solution, but is it?
No, of course it isn't. There is no absolute guarantee that the next byte after test
will be '\0'
(a string terminating char). What you, then, ought to do is create a wrapper string:
char char_wrapper[2] = {};//initializes according to standard
//but as Lundin pointed out, self-documenting code is important:
char_wrapper[0] = options.test;?
char_wrapper[1] = '\0';//explicit, so it's clear what this code does
print(char_wrapper);
That should work just fine.
You can, of course, write this as a one-liner:
char char_wrapper[2] = {options.test, '\0'};//same as before, only in 1 statement
print(char_wrapper);//print "string"
That should do it, really. You don't even have to explicitly write the terminating char, since the standard specifically states:
An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
6.7.8 Initialization cf p. 138, semantics, point 14
Be that as it may, I'd still prefer to browse the web, or just set about writing your own minor implementation of printf
so you can use format specifiers. Heck, it's one of the first exercises in the K&R book, and there's tons of solutions floating about on the net. check those out, and adapt them to your specific needs.
Or, perhaps define print
to accept a size_t
argument, to specify how many chars you want to pass to the output stream. and call it like so print(options.test, 1);
Upvotes: 1
Reputation:
print
is looking for a char*
. You are passing an char
which can also be represented as an int
. Thus, the function is trying to implicitly convert an int
to a pointer as it is telling you.
char ptr[2];
ptr[0] = options.test;
ptr[1] = '\0';
Would wrap the char
into a char
array, which in C will decay into a pointer when you pass it to a function.
Upvotes: 0
Reputation: 399919
Create a temporary 2-character long string that has the character to print, and then the terminator. Then pass that string to your print()
function:
void print_options(const struct my_options *opt)
{
char tmp[] = { opt->test, '\0' };
print(tmp);
}
Upvotes: 2
Reputation: 5934
Create a char array to hold your char and then print it:
char wrapper[2];
wrapper[0] = options.test;
wrapper[1] = '\0';
print(wrapper);
Upvotes: 3