K33rg4t3
Full Member level 3
- Joined
- Aug 2, 2015
- Messages
- 165
- Helped
- 7
- Reputation
- 14
- Reaction score
- 7
- Trophy points
- 1,298
- Activity points
- 2,607
typedef struct
{
char data[4];
} TEST;
void doTest(char *data)
{
char txt[64];
TEST *p;
UART1_Write(data); // writes ABC
p = (TEST*)data;
sprintf(txt,"%c %c",p->data[0], p->data[1]);
UART1_Write(txt); // writes A C !!
}
typedef struct
{
char engineRight;
char engineLeft;
char rotorA;
char rotorB;
} MyState;
void OnNewState(char *data)
{
char r, l, a, b;
// the data contents are OK here, I have tested it with UART
UART_Write(data);
// here is bug...
MyState *p = data;
r = p->engineRight;
l = p->engineLeft;
a = p->rotorA;
b = p->rotorB;
// and now you see, using p->engineLeft is returning bad value for me...
// rest of the code is not important
//SetEngineLeft(r);
}
#pragma pack(1)
typedef struct
{
char engineRight;
char engineLeft;
char rotorA;
char rotorB;
} MyState;
typedef struct {
char a;
char b;
char c;
char d;
} TestStruct;
typedef struct {
char arr[4];
} TestStruct2;
void main(void){
TestStruct *ptr;
TestStruct2 *ptr2;
char buffer[64] = { 'A', 'B', 'C', 'D', 0 };
char txt[64];
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators
// 25 is TX, 26 is RX
UART1_Init(300);
while(1)
{
Delay_ms(1000);
sprintf(txt,"Sizeof struct is %i\n\r",sizeof(TestStruct));
UART1_Write_Text(txt);
Delay_ms(1000);
ptr = (TestStruct*)buffer;
sprintf(txt,"Members: a %c, b %c, c %c, d %c\n\r",ptr->a,ptr->b,ptr->c,ptr->d);
UART1_Write_Text(txt);
Delay_ms(1000);
ptr2 = (TestStruct2*)buffer;
sprintf(txt,"Array in struct: [0] %c, [1] %c, [2] %c, [3] %c\n\r",ptr2->arr[0],ptr2->arr[1],ptr2->arr[2],ptr2->arr[3]);
UART1_Write_Text(txt);
Delay_ms(1000);
sprintf(txt,"Raw: %s\n\r",buffer);
UART1_Write_Text(txt);
}
}
Raw: ABCD
Sizeof struct is 4
Members: a A, b C, c , d
Array in struct: [0] A, [1] C, [2] , [3]
typedef struct {
char a;
char b;
char c;
char d;
} TestStruct;
typedef struct {
char arr[4];
} TestStruct2;
void dbg(char index,char a,char b,char c,char d){
// UART_Write(a);
// UART_Write(b);
// UART_Write(c);
// UART_Write(d);
//UART_Write('\n');
_asm nop;
}
void main(void){
TestStruct *ptr;
TestStruct2 *ptr2;
char buffer[5] = { 'A', 'B', 'C', 'D', 0 };
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators
// 25 is TX, 26 is RX
//UART1_Init(300);
while(1)
{
Delay_ms(1000);
dbg(1,'s','z',' ',sizeof(TestStruct)+'0');
Delay_ms(1000);
ptr = (TestStruct*)buffer;
dbg(2,ptr->a,ptr->b,ptr->c,ptr->d);
Delay_ms(1000);
ptr2 = (TestStruct2*)buffer;
dbg(3,ptr2->arr[0],ptr2->arr[1],ptr2->arr[2],ptr2->arr[3]);
Delay_ms(1000);
dbg(4,buffer[0],buffer[1],buffer[2],buffer[3]);
}
}
typedef struct {
char a;
char b;
char c;
char d;
} TestStruct;
typedef struct {
char arr[4];
} TestStruct2;
void dbg(char a, char b, char c, char d)
{
_asm nop;
}
void main(void){
TestStruct *ptr;
TestStruct2 *ptr2;
char buffer[64] = { 'A', 'B', 'C', 'D', 0 };
char txt[64];
char a, b, c, d;
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators
// 25 is TX, 26 is RX
UART1_Init(300);
while(1)
{
ptr = (TestStruct*)buffer;
// OK - a, b, c, d are 'A', 'B', 'C' and 'D'
a = ptr->a;
b = ptr->b;
c = ptr->c;
d = ptr->d;
// OK: variables in function are 'A', 'B', 'C' and 'D'
dbg(a,b,c,d);
// ERROR: txt contents are A,C, garbage...
sprintf(txt,"%c%c%c%c\n\r",a,b,c,d);
}
}
Suggests that the problem hasn't anything to do with structures or arrays. It would be approriate to mention the mikroC version. Are you running the latest version?Results: only the sprintf output is broken, which is stored in txt variable value.
Suggests that the problem hasn't anything to do with structures or arrays. It would be approriate to mention the mikroC version. Are you running the latest version?
Why don't you ask them...https://www.mikroe.com/ ???
sprintf(txt,"%c%",a);
sprintf(strend(txt),"%c",b);
sprintf(strend(txt),"%c",d);
sprintf(strend(txt),"%c\n\r",d);
Won't multiple sprintf() calls solve the problem?
Code:sprintf(txt,"%c%",a); sprintf(strend(txt),"%c",b); sprintf(strend(txt),"%c",d); sprintf(strend(txt),"%c\n\r",d);
typedef struct {
char a;
char b;
char c;
char d;
} TestStruct;
typedef struct {
char arr[4];
} TestStruct2;
void dbg(char a, char b, char c, char d)
{
_asm nop;
}
char * strend(char *p)
{
while(*p)
p++;
return p;
}
void main(void){
TestStruct *ptr;
TestStruct2 *ptr2;
char buffer[8] = { 'A', 'B', 'C', 'D', 0 };
char txt[8];
char a, b, c, d;
ADCON1 |= 0x0F; // Configure all ports with analog function as digital
CMCON |= 7; // Disable comparators
// 25 is TX, 26 is RX
UART1_Init(300);
while(1)
{
ptr = (TestStruct*)buffer;
// OK - a, b, c, d are 'A', 'B', 'C' and 'D'
a = ptr->a;
b = ptr->b;
c = ptr->c;
d = ptr->d;
// OK: variables in function are 'A', 'B', 'C' and 'D'
dbg(a,b,c,d);
// FvM work around
sprintf(txt,"%c%",a);
sprintf(strend(txt),"%c",b);
sprintf(strend(txt),"%c",c);
sprintf(strend(txt),"%c\n\r",d);
}
}
No problem, get the bug fixed by mikroE or use a industry-standard PIC compiler, e.g. XC8....but I want a real solution to the problem, not a work-around
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?