База знаний / Справочник TiOS / Разработка (TiOS) / Руководство по программированию / Tibbo BASIC/C / Переменные, указатели, константы, области видимости
Sizeof и Countof
Sizeof (Tibbo C)
В Tibbo C оператор sizeof() реализован стандартно.
Sizeof (Tibbo BASIC)
В Tibbo BASIC оператор sizeof() тоже есть, но в BASIC его можно применять только к типам (то есть к определениям структур Tibbo BASIC):
type t1_type
a as char
i as integer
end type
dim t1 as t1_type
sub on_sys_init
dim i as word
i=sizeof(t1_type) 'i will be 3 (char + integer)
i=sizeof(t1) 'this will generate error
end sub
Countof (только Tibbo BASIC)
Оператор countof() в BASIC позволяет получить число элементов в массивах и структурах. Для строковых переменных, не являющихся массивами, он также возвращает ёмкость строки.
Следующий пример показывает, как ведёт себя оператор countof().
dim l as dword
dim x(5) as byte
dim y(5,2) as word
dim s as string(20) 'a string variable with the capacity for 20 characters
dim ss(10) as string(20) 'an array of 10 string variables with the capacity of 20 characters each
type t1_type
a as char
i as integer
end type
dim t1 as t1_type
sub on_sys_init
dim i as word
i=countof(i) 'i will be 1 -- this dword variable occupies 4 bytes but countof() returns the number of members, not the size in memory
i=countof(x) 'i will be 5 -- the number of elements in the x array
i=countof(y) 'i will still be 5 -- the countof() operator can only see into the first array dimension
i=countof(s) 'i will be 20 -- the string's capacity
i=countof(ss) 'i will be 10 -- the number of array members; the countof() operator will not be able to see beyond this
i=countof(t1) 'i will be 2 -- t1 has two members
i=countof(t1_type) 'this will generate error
end sub