|
|
View previous topic :: View next topic |
Author |
Message |
jrd Guest
|
comparing structs |
Posted: Sun Nov 18, 2001 12:12 am |
|
|
I wish to compare two structs:
struct { ... } a;
struct { ... } b;
_bool mem_matches(void *a, void *b, int size)
{
for(;size>0;--size,++a,++b) if(*a!=*b) return false;
return true;
}
for some reason the compiler will not accept the
void * pointers. Further, if I change them to struct *'s,
the compiler hits if comparison and says field expected.
What gives? Why can't I compare structures this way?
Is it necessary to set up a union like this:
union {
struct {... } a;
char foo[sizeof(a)];
} x
?
Thank you!
Justin Dobbs
___________________________
This message was ported from CCS's old forum
Original Post ID: 1185 |
|
|
jrd Guest
|
Re: comparing structs |
Posted: Sun Nov 18, 2001 1:18 am |
|
|
I mistyped in my post. I meant to declare the variables
const void *. Also, I fixed my syntax and it seems to work
this way. I still wish I could use (const void *) to
compare with any pointer type.
typedef struct { ... } system_t;
system_t a, b;
#define _Bool int1
_Bool memory_regions_match(system_t *a, system_t *b, _Byte n)
{
for(;n>0;--n) if(*((unsigned char*)a++)!=*((unsigned char *)b++)) return FALSE;
return TRUE;
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 1186 |
|
|
Tomi Guest
|
Re: comparing structs |
Posted: Sun Nov 18, 2001 10:49 am |
|
|
There are some problems about the things you want to implement.
The CCS C is not for scientific applications; it is for programming PIC microcontrollers. PICs use Harward architecture. Pointers to the ROM space is not allowed so you can not use a "const type *" reference (consts are stored in the ROM).
The char type is unsigned in CCS C by default so your casting "(unsigned char*)" is not necessary.
Using "void *" as a "universal pointer" is not really supported. Pointers are always passed to a function as "pointers to fundamental data" what is "char *".
This means that you can freely use in your function something like this:
a[index]
So this snippet is working fine in CCS C:
typedef struct {
char a;
float b;
int16 c;
int32 d;
} MyStructType;
char memory_regions_match(char *a, char *b, char n)
{
for(;n>0;--n) if(a[n]!=b[n]) return FALSE;
return TRUE;
}
char c1,c2;
float f1,f2;
MyStructType s1,s2;
memory_regions_match(&c1,&c2,sizeof(char));
memory_regions_match(&f1,&f2,sizeof(float));
memory_regions_match(&s1,&s2,sizeof(MyStructType));
The list:
.................... memory_regions_match(&c1,&c2,sizeof(char));
0078: MOVLW 2A // address of c1
0079: MOVWF 4A // put it into SW stack
007A: MOVLW 2B // address of c2
007B: MOVWF 4B // put it into SW stack
007C: MOVLW 01 // the char size is 1
007D: MOVWF 4C // put it into SW stack
007E: CALL 04C // call the memory_regions_match function
.................... memory_regions_match(&f1,&f2,sizeof(float));
007F: MOVLW 2C
0080: MOVWF 4A
0081: MOVLW 30
0082: MOVWF 4B
0083: MOVLW 04 // the float size is 4
0084: MOVWF 4C
0085: CALL 04C
.................... memory_regions_match(&s1,&s2,sizeof(MyStructType));
0086: MOVLW 34
0087: MOVWF 4A
0088: MOVLW 3F
0089: MOVWF 4B
008A: MOVLW 0B // the sample struct size is 11
008B: MOVWF 4C
008C: CALL 04C
___________________________
This message was ported from CCS's old forum
Original Post ID: 1197 |
|
|
jrd Guest
|
Re: comparing structs |
Posted: Wed Nov 21, 2001 1:55 am |
|
|
:=There are some problems about the things you want to implement.
Your post was exactly what I needed. Thank you tom!
___________________________
This message was ported from CCS's old forum
Original Post ID: 1271 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|