C/C++ Programmer's Guide (G06.27+, H06.08+, J06.03+)

blocks. Without relocatable data blocks, static data is not allowed. Therefore, the compiler allocates
static data items such as constants in the user code space instead of the user or system data spaces.)
To pass pointers to such constants in TNS C and C++ programs, you must use the CSADDR pragma.
(Native C programs do not require the CSADDR pragma.) The CSADDR pragma directs the compiler
to allocate sufficient space on the stack for each code space data object pointed to by a function
argument. It generates move instructions to copy each data object to the corresponding implicit
stack address, based on information from previous data declarations. The new stack address,
instead of the code space address, is then passed as the function argument.
Example
This example shows valid uses of the CSADDR pragma to pass pointers to constants between code
spaces:
#pragma env library
#pragma csaddr
#pragma xmem
void foo( const void * );
main()
{
const struct S { int i,j,k; } s = { 10, 20, 30 };
const struct S *sp;
foo( "This is a test" ); /* 15 bytes copied and passed */
foo( &s ); /* 6 bytes copied and passed */
sp = &s;
A pointer variable to the code space cannot be initialized. Explicit assignment or conversion to
array type is recommended. For example, this function results in an error because name is an
array of pointers and therefore cannot be initialized:
#pragma env library
#include <stringh>
int find_kevin(void)
{
const char *name[] =
{"linda", "don", "kevin"};
const int LAST_NAME = 3;
const char *p;
int i = 0;
for (p = name; i <= LAST_NAME ; i++, p = &name[i])
if (!strcmp(p, "kevin"))
return TRUE;
return FALSE;
}
The previous function can be made valid by converting name to a two dimensional array without
making any other changes, as shown in this code:
#pragma env library
#pragma inline
#include <stringh>
int find_kevin(void)
{
const char name[][6] =
{{"linda"}, {"don"}, {"kevin"}};
const int LAST_NAME = 3;
const char *p;
int i = 0;
Passing Pointers to Constants Between Code Spaces 145