OpenVMS Source Code Demos

DESCRIPTOR_MACRO

//============================================================================================================
// title : descriptor_macro.c
// author: Neil Rieck
// date  : 2014-12-23
// notes : 1) the $DESCRIPTOR macro (found in <descrip.h>) is really dumb so be very careful when using it.
//         2) Why? It incorrectly uses "sizeof()-1" rather than "strlen()" which might not be what you wanted.
//         3) if in doubt, never use this macro. Populate the structure yourself.
//============================================================================================================
#include <stdio.h>					// standard C
#include <string.h>					// standard C
#include <descrip.h>					// VMS/OpenVMS systems only
//
char neil[] = "Neil Rieck";
char * ptr;
//
void main () {
    ptr = neil;						// copy address of static string
    //
    printf("var: neil[] sizeof: %d strlen: %d\n", sizeof(neil), strlen(neil));
    printf("var: *ptr   sizeof: %d strlen: %d\n", sizeof(ptr) , strlen(ptr));
    //
    //	create 3 new variables of type "struct dsc$descriptor_s"
    //
    $DESCRIPTOR (macro1, "SYS$COMMAND");		// example from the docs
    $DESCRIPTOR (macro2, neil);				// should work
    $DESCRIPTOR (macro3, ptr);				// does not work
    //
    printf("macro1 len %d \n",macro1.dsc$w_length);	// yields:  11 (correct answer)
    printf("macro2 len %d \n",macro2.dsc$w_length);	// yields:  10 (correct answer)
    printf("macro3 len %d \n",macro3.dsc$w_length);	// yields:   3 (incorrect answer)
    //
    printf("macro1 typ %d \n",macro1.dsc$b_dtype);	// yields:  14 DSC$K_DTYPE_T (character string)
    printf("macro2 typ %d \n",macro2.dsc$b_dtype);	// yields:  14 DSC$K_DTYPE_T (character string)
    printf("macro3 typ %d \n",macro3.dsc$b_dtype);	// yields:  14 DSC$K_DTYPE_T (character string)
    //
    printf("macro1 cla %d \n",macro1.dsc$b_class);	// yields:   1 DSC$K_CLASS_S (fixed-length descriptor)
    printf("macro2 cla %d \n",macro2.dsc$b_class);	// yields:   1 DSC$K_CLASS_S (fixed-length descriptor)
    printf("macro3 cla %d \n",macro3.dsc$b_class);	// yields:   1 DSC$K_CLASS_S (fixed-length descriptor)
}