LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-26-2013, 07:40 AM   #1
Mr Oblivion
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
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;
}

Last edited by acid_kewpie; 02-26-2013 at 07:55 AM. Reason: I forgot a key part.
 
Old 02-26-2013, 08:01 AM   #2
kheldar
Member
 
Registered: Jul 2004
Location: Nottingham, England
Distribution: Fedora 17, Centos 6.3 x86_64
Posts: 35

Rep: Reputation: 2
Exactly what it says on the tin! "n" is a pointer...

Quote:
Originally Posted by Mr Oblivion View Post
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.
 
1 members found this post helpful.
Old 02-26-2013, 08:02 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
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.
 
1 members found this post helpful.
Old 02-26-2013, 09:13 AM   #4
Mr Oblivion
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Lightbulb

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] comparison between pointer and integer texasone Programming 14 07-10-2010 08:42 AM
ISO C++ forbids comparison between pointer and integer BarryKamp Programming 5 04-25-2007 10:06 PM
comparison between pointer and integer a problem? debiant Programming 7 08-28-2006 08:57 PM
Comparison between pointer and integer ---> WHY?? its_godzilla Programming 10 01-28-2005 10:40 PM
C programming error. warning: comparison between pointer and integer Linh Programming 4 06-06-2003 04:49 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration