2

I have prompted the user for 5 inputs in the main and saved them into an array called array. I've checked the array in main and it prints out the values as they were entered. However, when I pass it to a function which I've included below, I get the output that the array contains all 0 values. What am I doing wrong?

conversion(float array[], int size)
{
float add = 0.0;
float change, num;

printf("\nThe array is: \n");
for (i=0;i < size;i++)
{
    printf("%.2f\n",&array[i]);
}


/*calculate and store the conversion values in a new array*/
for(i=0; i<s; i++)
{
    num = array[i];
    change = (num*100.50);
    for(j=0; j<1; j++)
    {
        printf("\n %.2f your number is %.2f in float percent\n", &num, &change);
    }
}
}
4
  • 2
    printf("%.2f\n",&cArray[i]); Remove the &. With it you are passing the address of the variable, when you want to pass the value. Commented Feb 21, 2018 at 22:07
  • Thank you, that did let me view the right print so at least now I know that the array is being passed. However, I'm still getting multiple "0.00 in C is 0.00 in F", do you have any advice for that? Commented Feb 21, 2018 at 22:09
  • Same issue there, too. Commented Feb 21, 2018 at 22:10
  • Thank you so much, that's fixed it. Sorry for the seemingly simple question. I'm only starting C after using Java and I'm having some issues with the print functions. I'm used to printing anything in some ++. And all this %d/%f/%c and & etc is just confusing still. Commented Feb 21, 2018 at 22:13

2 Answers 2

2
&cArray[i]

you don't need to address of the ith element in order to print it, you just need the ith element

Change

printf("%.2f\n",&cArray[i]);

printf("\n %.2f in Celsius is %.2f in Fahrenheit\n", &temp, &con);

to

printf("%.2f\n",cArray[i]);

printf("\n %.2f in Celsius is %.2f in Fahrenheit\n", temp, con);

The same with con, printf() doesn't need the address of a scalar variable in order to print it.

Sign up to request clarification or add additional context in comments.

Comments

0

Like Johnny Mopp said you have an extraneous &,

But depending on the size of the array you're dealing with you may just want to pass a pointer to the function instead.

1 Comment

In C, arrays are always passed as pointers, they are not copied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.