Incorrect result in c code

How your code should look

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 void myfunction(double x, int y[2], int *d);

int main(int argc, char **argv)
{
   int a[2];
    int *c = malloc(sizeof(int));
    double b=10.0;
    *c = 5;
    a[0]=1;
    a[1]=2;

    printf("before call %f %d %d %d\n",b,a[0],a[1],*c);
    myfunction(b,a,c);
    printf("after call %f %d %d %d\n",b,a[0],a[1],*c);
}

void myfunction(double x, int y[2], int *d)
{
    double z;
    x=2*x;
    y[0]=3*y[0];
    y[1]=3*y[1];
    *d =*d+2;
}

Note, malloc correct size

arrays start at 0

modern declarations of functions

declaring variables at time of first use.

fixed prrintf format (% not $)

Leave a Comment