/* * ============================================================== * Demonstrate dynamic allocation for an array of complex numbers * ============================================================== * * Copyright (C) 1993-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written By; M. Austin January 1994 * ============================================================== */ #include #include "miscellaneous.h" /* Data Structure for Array of Complex Numbers */ typedef struct { double dReal; double dImaginary; } COMPLEX; enum { NoComplexNumbers = 4 }; /* No of Items in Array of Complex Numbers */ int main( void ) { COMPLEX *spComplex1, *spComplex2; /* Pointers to data structures of type complex */ int ii; /* Counter for loop */ /* [a] : Instantiate array of complex numbers */ spComplex1 = (COMPLEX *) safeCalloc( NoComplexNumbers, sizeof(COMPLEX) , __FILE__, __LINE__ ); for(ii = 1; ii <= (int) NoComplexNumbers; ii++) { spComplex1[ ii-1 ].dReal = (double) (2.0 + ii); spComplex1[ ii-1 ].dImaginary = (double) (3.0 + ii); } /* [b] : Print Array via name_of_structure.name_of_member */ printf("\n*** Part 1 : Pring Array of Complex Numbers \n"); for(ii = 1; ii <= NoComplexNumbers; ii++) printf("*** ii = %3d : real = %9.3f imaginary = %9.3f\n", ii, spComplex1[ ii-1 ].dReal, (*(spComplex1 + ii - 1)).dImaginary); /* [c] : Print array via pointer cp2->member notation */ printf("\n*** Part 2 : Pring Array of Complex Numbers \n"); for(ii = 1; ii <= NoComplexNumbers; ii++) { spComplex2 = spComplex1 + ii - 1; /* same as : spComplex2 = &spComplex1[ii-1]; */ printf("*** i = %3d : real = %9.3f imaginary = %9.3f\n", ii, spComplex2->dReal, (spComplex1 + ii - 1)->dImaginary); } }