FutureBasic Logo

<<    Index    >> FutureBasic

concatCStr   statement



Syntax 1
concatCStr( targetCString, sourceCString1 [ , sourceCString2 [ ... ] )

Description
The concatCStr statement concatenates two or more C strings into a target C string.
The first argument is the target C string followed by the C strings to be concatenated, each separated by a comma ','

C strings used with concatCStr must be defined as either an array of characters ( i.e. char ) or array of bytes ( i.e. byte )

Note: the one line C string declaration and initialization works both inside functions ( as shown in the example ) and in main.

Example Showing Declaration & Initialization

local fn DoIt
char s1(3) = 'abc' // initialize a C string. String must be in single quotes not double quotes.
char s2(3) = 'def'
char s3(3) = 'hij'
char s4(10) = ''
byte b1(5) // just to show it's possible. Not used in this code
concatCStr( s4(0), s1(0), s2(0), s3(0) )
printf @"%s",@s4(0) // prints abcdefhij
end fn

fn DoIt

HandleEvents

Example Showing Declaration without Initialization

char s8(3) // if string array declared but not initialized, characters must be assigned separately
s8(0) = 65 : s8(1) = _"Y" : s8(2) = _"Z"
printf @"%s",@s8(0) // prints AYZ

char s5(3) = 'klm'// declaration & initialization works in main too
char s6(3) = 'nop'
char s7(3) = 'qrs'
char s8(10)

concatCStr( s8(0), s5(0), s6(0), s7(0) )
printf @"%s",@s8(0) // prints klmnopqrs

HandleEvents

Combining long strings without the line continuation character "¬"

// Initializer strings have unlimited length but they can't be combined with the line continuation character ( ¬ ) yet.
// Workaround: use concatCStr
char s5(100) = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
char s6(100) = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
char s7(100) = 'abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij'
char big(300)

concatCStr( big(0), s5(0), s6(0), s7(0) )

printf @"%s",@big(0) // prints 300 character string. pascal strings can't do this!

HandleEvents

Passing C Strings to a Function

local fn CStringsPassedToMe( cStr1(3) as char, cStr2(3) as char, returnedStr(10) as char )
concatCStr( returnedStr(0), cStr1(0), cStr2(0) )
printf @"returnedStr within CStringsPassedToMe is: %s",@returnedStr(0) // print abcdefhij
end fn

char s1(3) = 'abc' // initialize a CStr ( which is a character array )
char s2(3) = 'def'
char s3(3) = 'hij' // could put up to 700 but sizes to whatever is provided
char s4(10) = ''

fn CStringsPassedToMe( s1(0), s2(0), s4(0) )
printf @"s4 after returning from CStringsPassedToMe is: %s",@s4(0) // print abcdefhij

HandleEvents

Caveats
[a] C strings can represent only single byte characters - same as pascal strings.
[b] C strings can be any length, unlike pascal strings.
[c] C string arrays do not go stale like objects but follow local and global variable's access rules.
[d] C language offers other ways to define C strings as pointers. These are not available in FB yet.
[e] However, FB can pass( to a function for example ) a pointer to the C string.
[f] Unsigned types ( i.e. unsigned char ) are not available yet.
[g] All the C language library string functions( like fn strlen) are available too.

[h] This is not a complete C string implementation. Don't expect the same syntax FB provides for pascal strings ( yet ).