您需要为您的 GridDim 函数声明一个函数原型,并将其包含在包含您的 main 方法的文件中。
原因是除非编译器有一个声明参数类型的函数原型,否则它将把所有浮点数提升为双精度数。在运行时,您的浮点数被提升为双精度数,然后传递给 GridDim,后者读取双精度数的前半部分并将其解释为浮点数。如果您在 GridDim 中打印值,您将看到浮点数作为损坏的值出现。
如果您在头文件中声明您的方法,例如 GridDim.h:
#ifndef __GRID_DIM_DOT_H__
#define __GRID_DIM_DOT_H__
extern void GridDim(float long1, float long2, float dx, float lat1, float lat2, float dy, float depth1, float depth2, float dh, int* m, int* n, int* k);
#endif/*__GRID_DIM_DOT_H__*/
...并在 GridDim.c 中 #include (以确保定义与声明匹配):
#include <stdio.h>
#include "GridDim.h"
void GridDim(float long1, float long2, float dx,
float lat1, float lat2, float dy,
float depth1, float depth2, float dh,
int *m, int *n, int *k)
{
printf("long1 = %10.6f\n", long1);
printf("long2 = %10.6f\n", long2);
printf("dx = %10.6f\n", dx);
printf("lat1 = %10.6f\n", lat1);
printf("lat2 = %10.6f\n", lat2);
printf("long1 = %10.6f\n", dy);
printf("depth1 = %10.6f\n", depth1);
printf("depth2 = %10.6f\n", depth2);
printf("dh = %10.6f\n", dh);
*m = (int) ((long2-long1)/dx+1);
*n = (int) ((lat2-lat1)/dy+1);
*k = (int) ((depth2-depth1)/dh+1);
}
...并在 Main.c 中 #include 它以确保调用与声明匹配:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "GridDim.h"
int main()
{
int m,n,k;
GridDim(20.0f,30.0f,0.1f,10.0f,15.0f,0.1f,5.0f,20.0f,5.0f,&m,&n,&k);
printf("m= %d\nn= %d\nk= %d\n", m, n, k);
return 1;
}
...然后参数将正确传递给 GridDim 函数。我添加了一些 printf 语句,以便您可以看到这一点。
如果您在 Main.c 中注释掉 #include "GridDim.h",那么您将看到当前版本的代码中发生了什么:
long1 = 0.000000
long2 = 0.000000
dx = -0.000000
lat1 = 0.000000
lat2 = 0.000000
long1 = -0.000000
depth1 = 0.000000
depth2 = 0.000000
dh = 0.000000
m= 1
n= 1
k= -2147483648
使用#include,输出如下所示:
long1 = 20.000000
long2 = 30.000000
dx = 0.100000
lat1 = 10.000000
lat2 = 15.000000
long1 = 0.100000
depth1 = 5.000000
depth2 = 20.000000
dh = 5.000000
m= 101
n= 51
k= 4