Using pointers – Rockwell Automation 1771-DMC_DMC1_DMC4_DXPS Control Coprocessor User Manual User Manual

Page 133

Advertising
background image

API Library of Routines

Appendix B

Using Pointers

B-2

The C syntax section in this appendix provides, in C definition format, the
arguments for each function. In keeping with how functions are defined in
C, the syntax uses an asterisk (*) in the type declaration for an argument to
indicate that the function expects a pointer to the given type, for example:

unsigned DTL_DEF_AVAIL (num_avail)

unsigned *num_avail;

/*{a function definition would be here!} */

In actual practice, the pointer must point to some existing memory.
If the declaration above were used in an actual program, the pointer,

num_avail

, would not point to anything.

To emphasize this and provide a guide for use, the C example section for
each function will declare a variable of the required type (which allocates
memory for the variable) and will then use the “address of” (&) operator to
pass the address.

main ()

{

unsigned num_avail; /* allocated memory for variable! */

CC_INIT ();

.

.

.

DTL_DEF_AVAIL (&num_avail);

if (num_avail >0) /* OK */

.

.

.

}

When a function requires a pointer to a type, ensure that you pass the
address of an area of memory that you have created.

char *pointer_only;

/* NO */

char allocated_array [100];

/* YES! */

char *pointer_only = “string constant has an address \n”

/* YES */

When a function expects a pointer to any type or to a type that depends on
other arguments, the C syntax definition uses the pointer-to-void syntax.

void *ptr_to_some_unknown_type;

Your program should pass a pointer to a type that matches the data
being processed.

Using Pointers

Advertising