C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
// Now call the changeSome function, passing the value of i
// and the address of x (hence, the &)
changeSome(i, &x, iAry);
puts("\n\nHere are main()'s variables after the function:");
printf("i is %d\n", i);
printf("x is %.1f\n", x);
for (ctr = 0; ctr < 5; ctr++)
{
printf("iAry[%d] is %d\n", ctr, iAry[ctr]);
}
return(0); // Ends the program
}
/******************************************************************/
changeSome (int i, float *newX, int iAry[5])
{
// All variables are changes, but only the float and array
// remain changed when the program returns to main()
// changed when the program returns to main()
int j;
i = 47;
*newX = 97.6; // Same location as x in main
for (j = 0; j < 5; j++)
{
iAry[j] = 100 + 100*j;
}
return; // Returns to main
}

Here is the output from the program:


Click here to view code image


Here are main()'s variables before the function:
i is 10
x is 20.5
iAry[0] is 10
iAry[1] is 20
iAry[2] is 30
iAry[3] is 40
iAry[4] is 50
Here are main()'s variables after the function:
i is 10
x is 97.6
iAry[0] is 100
iAry[1] is 200
iAry[2] is 300
iAry[3] is 400
iAry[4] is 500
Free download pdf