Copying between Arrays, Structures, and Unions

Top  Previous  Next

In Tibbo BASIC and C, you can copy entire arrays and structures. In C, you can additionally copy unions.

For arrays, copying is only allowed between arrays of identical types.

Destination and source array sizes need not be equal — TiOS will only copy what fits in the destination or what's available in the source, depending on which array is smaller.

For structures (and also unions in C), copying is only allowed between the structures of the same type.

 

Tibbo BASIC:

Tibbo C:

dim ar1(10) as word
dim ar2(5) as word
dim ar3(12) as word
dim ar4(10) as byte
 
ar2=ar1         'this will work, 5 elements will be copied
          '(the destination is smaller than the source)
ar3=ar1         'this will work, 10 elements will be copied
          '(the source is smaller than the destination)
ar4=ar1         'this will generate compiler error ("type mismatch") --
          'you cannot copy from an array of words into an array of bytes
 
type othertype
  ar(2) as integer
  s as string(4)
end type
 
type bastype         'this type includes a variable ot of the othertype
  x as byte
  ot as othertype
end type
 
dim ot as othertype
dim bt as bastype
 
ot=bt.ot         'this will work, the source and the destination are of the same type
ot=bt           'this will generate compiler error ("type mismatch")

unsigned int ar1[10];
unsigned int ar2[5];
unsigned int ar3[12];
unsigned char ar4[10];
 
ar2=ar1;         //this will work, 5 elements will be copied
          //(the destination is smaller than the source)
ar3=ar1;         //this will work, 10 elements will be copied
          //(the source is smaller than the destination)
ar4=ar1;         //this will generate compiler error ("cannot cast") --
          //you cannot copy from an array of words into an array of bytes
 
struct otherstruct{
  unsigned int ar[2];
  string<4>s;
};
 
struct cstruct{     //this type includes a variable ot of the othertype
  unsigned char x;
  otherstruct os;
};
 
otherstruct os;
cstruct cs;
 
os=cs.os;         //this will work, the source and the destination are of the same type
os=cs;         //this will generate compiler error ("cannot cast")

 

'unions are only supported in C

struct pod_struct1{
  int i;
  int j;
}ps1;
 
struct pod_struct2{
  char c;
  long l;
}ps2;
 
union pod_union{
  pod_struct1 ps1;
  pod_struct2 ps2;
}pu;  
 
ps1=pu.ps1;         //this will work, the source and the destination are of the same type
ps1=pu.ps2;         //this will generate compiler error ("cannot cast")