LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   warning: comparison between pointer and integer (https://www.linuxquestions.org/questions/programming-9/warning-comparison-between-pointer-and-integer-4175451739/)

Mr Oblivion 02-26-2013 06:40 AM

warning: comparison between pointer and integer
 
Hello all,

I'm still quite new to programming, and are merely trying to create a program to calculate and display the Sum and Average of an array of values. However, when I compile I get the Error:
AverageWorking1.c:43:6: warning: comparison between pointer and integer
AverageWorking1.c:43:15: warning: comparison between pointer and integer

Line 43: if (n>100 || n<2)

Code:

#include <stdio.h>

int main()
{
        int n, temp, i=0;
        float num[100], sum=0.0, average;
        char c;

printf("This program calculates the Sum and Average of an array of values.\nEnter the number of        values in the array(2-100): ");

if(GetUserInput(&n))
{
if(temp!=1)
        while(i<n)
                {
                ++i;
                printf("%d. Enter number: ",i);
                scanf("%f",&num[i]);
                sum+=num[i];
                }

        average=sum/n;
        printf("Sum = %.2f\n",sum);
        printf("Average = %.2f\n",average);

return 0;
}
}

int GetUserInput(int* n)
{
        int temp;
        char c;

        temp =        scanf("%d", n);
        while((c = getchar()) != '\n' && c != EOF);

if(temp!=1)
        {
          printf("Error! Number should be in range of (2 to 100)\n");
        return 0;
        }
if (n>100 || n<2)
        {
          printf("Error! Number should be in range of (2 to 100).\n");
        return 0;
        }

return 1;
}


kheldar 02-26-2013 07:01 AM

Exactly what it says on the tin! "n" is a pointer...
 
Quote:

Originally Posted by Mr Oblivion (Post 4900001)
Hello all,

I'm still quite new to programming, and are merely trying to create a program to calculate and display the Sum and Average of an array of values. However, when I compile I get the Error:
AverageWorking1.c:43:6: warning: comparison between pointer and integer
AverageWorking1.c:43:15: warning: comparison between pointer and integer

Line 43: if (n>100 || n<2)

Since within your GetUserInput function, "n" is a pointer to an integer, you need it to say
Code:

if (*n>100 || *n<2)
so that the compiler knows you want to look at the value pointed to by "n", not the pointer "n" itself.

acid_kewpie 02-26-2013 07:02 AM

so n is a pointer. It holds the location in memory where a value lives. n does NOT contain the number. It "points" to where a number is.

To make this work, the if statements shoudl just be changed to reference "*n" instead of "n", but I would personally look to make GetUserInput(int* n) take the n directly instead (your &n when calling it creates the pointer reference) so tweaks like:

if(GetUserInput(&n)) ---> if(GetUserInput(n))
GetUserInput(int* n) ---> GetUserInput(int n)
temp = scanf("%d", n); ---> temp = scanf("%d", &n);

etc.

Note that you've used scanf differently here, once time you give it the variable, num, the other you create a pointer reference from n, &n.

Mr Oblivion 02-26-2013 08:13 AM

Ah, of course!

Thank you for all the help!
I feel such a fool, but I am still getting used to pointers. :L

-Mr Oblivion


All times are GMT -5. The time now is 01:26 AM.