Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 4 months ago.
Error at compiling
I want to print this variable :
main
printf(ubidotsGET(TOKEN, VARIABLE_ID)); //Line:18
and I´m getting this errors:
- Error: Argument of type "const char *" is incompatible with parameter of type "std::uint8_t *" in "main.cpp", Line: 18, Col: 24
- Error: Argument of type "const char *" is incompatible with parameter of type "std::uint8_t *" in "main.cpp", Line: 18, Col: 31
- Error: Argument of type "std::uint8_t *" is incompatible with parameter of type "const char *" in "main.cpp", Line: 18, Col: 13
That variable comes from a .h and .c files:
library.c
uint8_t* ubidotsGET( uint8_t* token, uint8_t* variable_id ){ memset(&buffer, 0,sizeof(buffer)); sprintf(buffer,"http://www.things.com/variables/%*s/?token=%*s",sizeof(variable_id),variable_id,sizeof(token),token); return buffer;}
and
library.h
#define TOKEN "123a" #define VARIABLE_ID "abc2" uint8_t* ubidotsGET( uint8_t* token, uint8_t* variable_id );
Any idea what im doing wrong, I imported this from a c library on mplab?
1 Answer
7 years, 4 months ago.
Hello Daniel,
printf
function is declared as:
int printf ( const char * format, ... );
So you have to pass it a const char*
type.
Find buffer
and make sure it's a const char*
type for example as follows:
char buffer[256]; // the size could be different
NOTE: Array type variables are always const
pointers.
Then change:
library.h
#define TOKEN "123a" #define VARIABLE_ID "abc2" const char* ubidotsGET(const char* token, const char* variable_id );
and
library.c
const char* ubidotsGET(const char* token, const char* variable_id ){ memset(&buffer, 0,sizeof(buffer)); sprintf(buffer,"http://www.things.com/variables/%*s/?token=%*s",sizeof(variable_id),variable_id,sizeof(token),token); return buffer; }
Another option is to modify only
library.c
uint8_t* ubidotsGET(const char* token, const char* variable_id ){ memset(&buffer, 0,sizeof(buffer)); sprintf(buffer,"http://www.things.com/variables/%*s/?token=%*s",sizeof(variable_id),variable_id,sizeof(token),token); return (uint8_t*)buffer; }
and format the variable passed to print as string:
main.cpp
pc.printf("%s", ubidotsGET(TOKEN, VARIABLE_ID));
NOTE: %d would not work correctly since it formats the variable as signed decimal integer.
I tried and i got some other error, this is the whole code https://developer.mbed.org/users/dan_cuspi/code/C-Conv/
posted by 01 Sep 2017Sorry I did not notice it before, but since the ubidotsGET
function is implemented in a standard 'C' file (library.c) but called from a 'C++' file (main.cpp) so the compiler applies function name mangling and that's why the linker is not able to find it. There are two solutions:
- Either Modify the declaration of
ubidotsGET
function in the 'library.h' file as follows:
library.h
#ifdef __cplusplus extern "C" { #endif uint8_t* ubidotsGET(const char* token, const char* variable_id); #ifdef __cplusplus } #endif
- Or rename the 'library.c' file to 'library.cpp', and (in order to match the function definition) modify the function declaration as below:
library.h
uint8_t* ubidotsGET(const char* token, const char* variable_id);
NOTE: The compiler will still report a warning "argument of type "uint8_t*" is incompatible with parameter of type "char*" but finish with success and the program will work. However, the warning could be removed if you use the first option proposed in my first answer (but you should rename the 'library.c' file to 'library.cpp'). Then in the 'main.cpp' you can call either:
main.cpp
pc.printf(ubidotsGET(TOKEN, VARIABLE_ID));
or
main.cpp
pc.printf("%s", ubidotsGET(TOKEN, VARIABLE_ID));
with the same result.
posted by 01 Sep 2017