База знаний / Справочник TiOS / Разработка (TiOS) / Руководство по программированию / Tibbo BASIC/C / Переменные, указатели, константы, области видимости / Массивы, структуры, объединения
Копирование между массивами, структурами и объединениями
В Tibbo BASIC и C можно копировать массивы и структуры целиком. В C дополнительно можно копировать объединения.
Массивы можно копировать только между массивами одинаковых типов. Размеры массива-приёмника и массива-источника могут не совпадать: TiOS скопирует только то, что помещается в приёмник или что есть в источнике, в зависимости от того, какой массив меньше.
Структуры (а в 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")
'unions are only supported in C
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")
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")