Sambar Server Documentation

CScript Pointers and Arrays


Basically, a pointer is a reference to an address in memory that contains some data type, such as an integer, float or string. A pointer to a data type is declared the same way as any other variable, except that an asterix (*) is placed before the name of the variable to indicate that it is a pointer. When refering to the pointer, we use another asterix to indicate we are dealing with the data referenced by the pointer, rather than the pointer itself.

The following example contains a pointer to an integer. We assign to our pointer the address (&) of an integer, and then modify the data stored there.
void main() { int number; int *pointer_to_number; number = 5; printf("Our number is currently %d\n", number); /* Assign to our pointer the address of variable 'number' */ pointer_to_number = &number; /* Modify the value stored at pointer_to_number */ *pointer_to_number = *pointer_to_number + 1; printf ("Our number is now %d\n", number); }

When run, the value of number is incremented, even those we never added to number directly. Instead, we changed the value stored at the memory address shared by number, thereby indirectly modified it. Unlike C, CScript prevents you from declaring a pointer and assign it to an invalid memory address. All pointer references are tested for validity prior to allowing their use (preventing users from overwriting data belonging to some other variable or piece of code).

Pointers and Arrays

The following example illustrates through the use of strings (which are defined as a null-terminated array of type char) that arrays are actually just pointers. When you have a printf statement, such as printf ("%s\n", mystring); you are actually passing as a parameter the address of a sequence of characters.
void main() { /* 80 bytes of memory */ char mystring[80], *mypointer; /* Copy a dummy string in */ strcpy (mystring, "Hello there!"); /* Since an array is already a pointer, we don't need to use &mystring */ mypointer = mystring; /* Prove its a pointer by printing it */ printf ("%s\n", mypointer); /* Print string at offset 6 from mypointer */ printf ("%s\n", mypointer + 6); }

When run, the program prints the words "Hello there!" to screen, followed again by a second "there!". There are two print statements, one for mypointer, and another for mypointer + 6. Since a pointer is just a memory address, we can add to this address just like any other number. Remember though that in C, arrays are indexed starting at zero (0).

The following initializes an array containing 6 integers:

int my_array[] = { 43, 22, 25, 5, -5, 100 };

Each of the integers can be referred to by means of a subscript to my_array, i.e. using my_array[0] referrs to the integer 43. Since CScript allows arbitrary types to be stored in arrays, the array definition can include strings, for example:
aa = "Test String";
my_array[] = { 43, "test", NULL, 5.5, -5, aa };

© 2001 Sambar Technologies. All rights reserved. Terms of Use.