Re: Inline::C Cookbook addition

[email protected] Thu, 8 Dec 2016 18:08:18 +1100
Newsgroups perl.inline
Message-ID <BA10447AAD8845539D1873C93065D054@OwnerPC311012>
From: Ron Grunwald via inline
Sent: Thursday, December 08, 2016 1:44 AM
To: [email protected]
Subject: Inline::C Cookbook addition
Hello all,

> The code example:
>
> use strict;
> use warnings;
> use Inline C => Config => ccflagsex => "-std=c99";
> use Inline C => "DATA";
>
> our $mesh_data = "MESH-POINTS 0.0 0.0 0.5 0.25 1.0 0.5 1.5 0.75";
> CalcSurfaceHeights();
>
> __DATA__
> __C__
> #define N_MP 4
>
> void CalcSurfaceHeights() {
>    double x[N_MP], y[N_MP], z;
>    char   *mesh_data = SvPV_nolen(get_sv("main::mesh_data", 0));
>
>    sscanf(mesh_data, "MESH-POINTS %lf%lf%lf%lf%lf%lf%lf%lf",
>                      x, y, x+1, y+1, x+2, y+2, x+3, y+3);
>
>    for (int ix=0; ix < N_MP; ix++) {
>       z = 0.5*( sin(x[ix]) + sin(y[ix]) );
>
>       printf("Surface-Height: %6.3f Mesh-Point: %6.2f, %6.2f\n",
>           z, x[ix], y[ix]);
>    }
> }

The "use Inline C => Config => ..." line can be removed if you rewrite the 
__C__ section as:

__DATA__
__C__
#define N_MP 4

void CalcSurfaceHeights() {
   double x[N_MP], y[N_MP], z;
   int ix;
   char   *mesh_data = SvPV_nolen(get_sv("main::mesh_data", 0));

   sscanf(mesh_data, "MESH-POINTS %lf%lf%lf%lf%lf%lf%lf%lf",
                     x, y, x+1, y+1, x+2, y+2, x+3, y+3);

   for (ix=0; ix < N_MP; ix++) {
      z = 0.5*( sin(x[ix]) + sin(y[ix]) );

      printf("Surface-Height: %6.3f Mesh-Point: %6.2f, %6.2f\n",
             z, x[ix], y[ix]);
   }
}

All I've done is move the declaration of "ix" from the for() loop to the 
declaration section at the beginning of CalcSurfaceHeights().
This then enables the code to compile using compilers (such as Microsoft 
compilers) that are not C99-compliant.

This mailing list is very quiet these days and I think your request will 
probably fall "through the cracks" unless you submit it as a github "Issue" 
or a github pull request.

Cheers,
Rob