Error when compiling for MicroChip 'invalid operands to binary ==' - c

static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt,coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
{
if (inpkt->payload.len == 0)
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
if (inpkt->payload.p[0] == '1')
{
light = '1';
UARTWrite(1,"ON\n");
return coap_make_response(scratch, outpkt, (const UINT8_VAL *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
else
{
light = '0';
UARTWrite(1,"OFF\n");
return coap_make_response(scratch, outpkt, (const UINT8_VAL *)&light, 1, id_hi, id_lo, COAP_RSPCODE_CHANGED, COAP_CONTENTTYPE_TEXT_PLAIN);
}
}
This is my code and i am getting error on line no.5 . The struct is defined as
typedef struct
{
coap_header_t hdr;
coap_buffer_t tok;
uint8_t numopts;
coap_option_t opts[MAXOPT];
coap_buffer_t payload;
} coap_packet_t;
typedef struct
{
const UINT8_VAL *p;
size_t len;
} coap_buffer_t;
I am getting the following error when i try to compile using Microchip c30 compiler which is primarily C compiler.
Error :- error: invalid operands to binary ==
Please HElp me !!

Try using the Val member of UINT8_VAL for comparison:
inpkt->payloadp[0].Val == '1'

Related

Confilicting Types [duplicate]

This question already has answers here:
Getting "conflicting types for function" in C, why?
(11 answers)
Closed 2 years ago.
I have a program that reads from an I2C pressure sensor. I'm getting the following error when I build the project: "conflicting types for 'PressureSensorRd'"
void getI2CPumpPressure(UINT32 index)
{
uint16_t value = 0;
uint8_t delete = 0;
I2C_RESULT result;
result = PressureSensorRd(delete, &value);
}
STATIC_DECL I2C_RESULT PressureSensorRd(uint8_t devReg8, uint16_t *pRdDataDest)
{
I2C_RESULT result;
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
if (result != I2C_RESULT_SUCCESS)
{
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
if (result != I2C_RESULT_SUCCESS)
{
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
if (result != I2C_RESULT_SUCCESS)
{
result = I2cRdUint16FromReg8(Pressure_Sensor_I2C_DEV_ADR, devReg8, pRdDataDest);
}
}
}
return (result);
}
You attempt to call PressureSensorRd before it is declared. As a result, the function is implicitly declared as int PressureSensorRd(). This conflicts with the actual definition of the function.
You need to add a declaration for PressureSensorRd before getI2CPumpPressure:
STATIC_DECL I2C_RESULT PressureSensorRd(uint8_t devReg8, uint16_t *pRdDataDest);
void getI2CPumpPressure(UINT32 index)
{
uint16_t value = 0;
uint8_t delete = 0;
I2C_RESULT result;
result = PressureSensorRd(delete, &value);
}
STATIC_DECL I2C_RESULT PressureSensorRd(uint8_t devReg8, uint16_t *pRdDataDest)
{
...

Can I access my union using the arrow operator (->)?

I would like to know whether or not I can use the arrow operator in my union and I don't have access to my build environment.
Let's say that I have the following union
union max30205_raw_data {
struct {
uint8_t lsb;
uint8_t msb;
};
struct {
uint16_t magnitude_bits:15;
uint16_t sign_bit:1;
};
uint16_t uwrd;
int16_t swrd;
};
I will fill the content of the union in the following way, but I was wondering whether the access to the union member msb and lsb is correct?
int32_t max30205_read_reg16_WithDefUnion(char reg, max30205_raw_data *p_unionRawData) {
char aux[] = {0,0}
int32_t error;
if (reg == MAX30205_REG_TEMPERATURE || reg == MAX30205_REG_THYST_LOW_TRIP || reg == MAX30205_REG_TOS_HIGH_TRIP) {
error = twi_max30205_read(&myMax30205Instance,max30205Address,reg,&aux,sizeof(aux));
if(error == 0){
p_unionRawData->msb = aux[0];//IS THIS RIGHT IN C?
p_unionRawData->lsb = aux[1];//IS THIS RIGHT IN C?
}
}
return error;
}
I will call max30205_read_reg16_WithDefUnion()as
int16_t max30205MeasureTemperatureWithDefUnion(void) {
char regT = MAX30205_REG_TEMPERATURE;
max30205_raw_data rawTemp;
rawTemp.msb = 0;
rawTemp.lsb = 0;
rawTemp.swrd = 0;
int16_t temperatureValue = 0;
if (max30205_read_reg16_WithDefUnion(regT,&rawTemp) ==0)
temperatureValue = rawTemp.swrd;
return temperatureValue;
}
Yes. -> works with both pointers to structs and pointers to unions.

How to programmatically map integers to const strings?

I have a bunch of error codes (0,1,10,11,20,30,40,...) that I need to map to their corresponding error messages. Since the error codes cannot conveniently be used as indeces into an array (it would be sparse and wasteful), I am thinking that this could somehow be accomplished with macros and/or enums.
I am basically trying to create a function const char *my_strerror(int errorcode).
const char *err00 = "an error message";
const char *err01 = "a different one";
const char *err10 = "another one";
const char* chatter_strerror(int error){
switch(error){
case 0:
return err00;
case 1:
return err01;
case 10:
return err10;
.... // 10 more cases
}
}
Surely there is a more elegant way to do this?
One solution would be to create an error message structure that contains an int field for the error code, and a char * field for the error message. Then an array of error message structs can be initialized with error codes and messages. This approach makes it easy to update the code with new error messages, and if the final struct in the array of error messages is used as a sentinel with a null pointer in the .msg field, functions which iterate over the array will not need to know how many elements it contains.
Here is an example. The get_error() function loops over the array, breaking out of the loop when the desired error code is encountered. If the sentinel value is reached and no match has been found, an "Unrecognized error code" message is returned. Note that there is no need to modify the get_error() function as new error messages are added to the error_codes[] array.
#include <stdio.h>
struct Errors
{
const char *msg;
int code;
};
struct Errors error_codes[] = {
{ .code = 1, .msg = "input error" },
{ .code = 5, .msg = "format error" },
{ .code = 10, .msg = "allocation error" },
{ .msg = NULL }
};
const char * get_error(int err_code);
int main(void)
{
printf("Error: %s\n", get_error(1));
printf("Error: %s\n", get_error(5));
printf("Error: %s\n", get_error(10));
printf("Error: %s\n", get_error(-1));
return 0;
}
const char * get_error(int err_code)
{
struct Errors *current = error_codes;
const char *ret_msg = "Unrecognized error code";
while (current->msg) {
if (current->code == err_code) {
ret_msg = current->msg;
break;
}
++current;
}
return ret_msg;
}
OP has specified int error codes, but also mentioned enums. Here is a modification using an enum. One advantage of using an enum here is increased readablility. A disadvantage is that now code must be modified in two places when error messages change.
#include <stdio.h>
/* Modify both the Error_Codes enum and the following error_codes[] array
when adding new error messages. */
enum Error_Codes {
ERRINPUT = 1,
ERRFORMAT = 5,
ERRALLOC = 10
};
struct Errors
{
const char *msg;
enum Error_Codes code;
};
struct Errors error_codes[] = {
{ .code = ERRINPUT, .msg = "input error" },
{ .code = ERRFORMAT, .msg = "format error" },
{ .code = ERRALLOC, .msg = "allocation error" },
{ .msg = NULL }
};
const char * get_error(enum Error_Codes err_code);
int main(void)
{
printf("Error: %s\n", get_error(ERRINPUT));
printf("Error: %s\n", get_error(ERRFORMAT));
printf("Error: %s\n", get_error(ERRALLOC));
printf("Error: %s\n", get_error(-1));
return 0;
}
const char * get_error(enum Error_Codes err_code)
{
struct Errors *current = error_codes;
const char *ret_msg = "Unrecognized error code";
while (current->msg) {
if (current->code == err_code) {
ret_msg = current->msg;
break;
}
++current;
}
return ret_msg;
}
Program output:
Error: input error
Error: format error
Error: allocation error
Error: Unrecognized error code

Why does an error occur when running the program?

I have an error and I can't find a method to solve it.I get this error
Exception thrown at 0x504A3E6C (ucrtbased.dll) in
ConsoleApplication3.exe: 0xC0000005: Access violation reading location
0x0047617A. On line 11.
#include "Entities.h"
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Expense* createExp(int nr_ap, float price, char* type) {
Expense *e = malloc(sizeof(Expense));
e->nr_ap = nr_ap;
e->price = price;
e->type = malloc(sizeof(char)*strlen(type) + 1); #Here is the problem.
strcpy(e->type, type);
return e;
}
void destroy(Expense *e) {
free(e->type);
free(e);
}
int getAp(Expense *e) {
return e->nr_ap;
}
float getPrice(Expense *e) {
return e->price;
}
char* getType(Expense *e) {
return e->type;
}
/*
Create a copy
*/
Expense *copyExp(Expense *e) {
return createExp(e->nr_ap, e->price, e->type);
}
void testCreateExp() {
Expense *e = createExp(10, 120, 'Gaz');
assert(getAp(e) == 10);
assert(getPrice(e) == 12);
assert(getType(e) == "Gaz");
destroy(e);
}
int main() {
testCreateExp();
}
Expense *e = createExp(10, 120, 'Gaz'); Makes no sense. single quote are used for single characters, not C strings.
e.g. char initial = 'G'; char* name = "Gaz";
Try Expense *e = createExp(10, 120, "Gaz");. Most compilers should give you a warning that using a single quote is not right in this context.
Also suspect your asserts are not "as expected" assert(getType(e) == "Gaz"); - shouldn't that be a strcmp() ?

Expected identifier before token in function pointer

typedef enum
{
TCP = 1,
UDP
}protocol;
typedef enum
{
DLL_Operation = 1,
MT_Operation,
Fork_Operation,
IPC_Operation
}msgc;
struct f
{
int seqNo;
protocol p;
msgc m;
protocol q;
int PayLoadSize;
void (*payload_ptr)();
};
This is my structure which i am using...
Now i am assigning address of function in that function pointer defining in strucutre...
if(f2.m == 1)
{
f2.(*payload_ptr) = DLL;
f2.payload_ptr();
}
else if(f2.m == 2)
{
f2.(*payload_ptr) = MT;
f2.payload_ptr();
}
else if(f2.m == 3)
{
f2.(*payload_ptr) = Fork;
f2.payload_ptr();
}
else
{
f2.(*payload_ptr) = IPC;
f2.payload_ptr();
}
in compiling this program... it is showing error like..
error: expected identifier before ‘(’ token
f2.(*payload_ptr) = DLL;
& same for all condition.... what is the solution..
this DLL, MT all are some function which i define for certain operation...
You are assigning the values to function pointers wrongly.
It should be like below for all the cases
if(f2.m == 1)
{
f2.payload_ptr = DLL;
f2.payload_ptr();
}
Please ensure that functions like DLL are of type void DLL();

Resources