C function float argument takes different value inside the function -
i sure i'm missing obvious here can't figure out is. in my repository, have file called ddpoly_2.c contains function called "ddpoly2". call function main function in tst5.c. if @ code in tst5_2.c, assigning x value of 2, printing , passing "ddpoly2" third argument (which per calling pattern should far can tell). print x within function "ddpoly2". see following:
x outside: 2.000000 x inside : 0.000000 nc: 2 nd: 3
you can see x 2.000000 before function called became 0.0000000 once inside function. must missing overtly obvious here can't figure out is.
p.s. compile code using makefile in same directory.
edit: including relevant parts of code here.
calling function in tst5_2.c:
int main(int argc, char *argv[]) { //used determine section gets results printed. float c[] = {1,1,1,0,0}; int nc = 2; float x = 2.0; float pd[] = {0,0,0,0,0}; int nd = 3; printf("x outside: %f\n",x); ddpoly2(c,nc,x,pd,nd); }
printing inside function ddpoly2:
#include <stdio.h> void ddpoly2(float c[], int nc, float x, float pd[], int nd) { int nnd, j, i; float cnst = 1.0; printf("x inside : %f\n",x); printf("nc: %d\n",nc); printf("nd: %d\n",nd); }
you're calling function without prototype. illegal since 1999 compiler helpful , allows compatibility old c standards.
the c standard says:
if expression denotes called function has type not include prototype, integer promotions performed on each argument, , arguments have type float promoted double.
the correct solution to:
- always have correct function prototypes.
- always enable warnings in compilation , treat them errors.
Comments
Post a Comment