LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-02-2013, 01:13 PM   #1
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Rep: Reputation: Disabled
warning: comparison between pointer and integer


struct node
{
char prod[20];
struct node *lt,*rt;
}*n;

void display(struct node *root,int h)
{
int i;
if(root!=NULL)
{
display(root->rt,h+1);
for(i=0;i<n;i++)
printf("\t");
printf("%s",root->prod)
display(root->lt,h+1);
}
}

this is just a part of the code. when i run the code i get this
" warning: comparison between pointer and integer [enabled by default]" line number 13
 
Old 05-02-2013, 02:54 PM   #2
mlslk31
Member
 
Registered: Mar 2013
Location: Florida, USA
Distribution: Slackware, FreeBSD
Posts: 210

Rep: Reputation: 76
You've declared n to be a pointer of type "struct node" and i as an int, so there's a comparison between pointer and integer.
 
Old 05-02-2013, 03:35 PM   #3
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
How do i correct it?
 
Old 05-02-2013, 03:41 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by lxmirmsh View Post
for(i=0;i<n;i++)
Did you write this code?
If so, what did you intend i<n to mean?

You have n declared as a pointer to a struct, not as a number, so the use of n in that for statement makes no sense.

If you copied code from someone else, I think you missed a declaration of n in the current scope.

Last edited by johnsfine; 05-02-2013 at 03:43 PM.
 
Old 05-02-2013, 04:02 PM   #5
mlslk31
Member
 
Registered: Mar 2013
Location: Florida, USA
Distribution: Slackware, FreeBSD
Posts: 210

Rep: Reputation: 76
You correct it by doing something else useful with the struct. Access one of its members, or get the address of the struct. One of the few things that might be useful with just n is to compare it to another "struct node", but to compare it to an integer that starts with 0 might not be what you want. Or is it? This is why my previous answer was so short.

For a lot of things in C, you can cast from one type to another, as in this:

Code:
n = (struct node *)malloc( sizeof( struct node ));
This doesn't seem to be one of those times.
 
Old 05-02-2013, 04:30 PM   #6
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 172

Rep: Reputation: 25
That for loop is strange:
Code:
for(i=0;i<n;i++)
This thing would repeatedly print one tabulator character.
To make this fact more clearly visible, i would really suggest to indent the following printf-line, as this will be the only thing that would be executed as body of the for() loop.

And then, how many TAB characters do you intend to print?

You probably want to remove the for() line entirely?
 
Old 05-02-2013, 04:34 PM   #7
bloody
Member
 
Registered: Feb 2013
Location: Berlin
Distribution: Gentoo, Debian
Posts: 172

Rep: Reputation: 25
Addendum: maybe "h" means "number of horizontal tabulators"? Then you should replace that "i<n" with "i<h", as "h" is not used anywhere else in the code? Anyhow, strange code snippet.
 
Old 05-02-2013, 07:23 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by bloody View Post
you should replace that "i<n" with "i<h"
You are correct. I didn't look at the OP's code carefully enough the first time to see that.

Quote:
maybe "h" means "number of horizontal tabulators"?
h represents how many levels of the sideways tree being printed are to the left of the subtree currently being printed. It is a recursive tree printing routine. That is what I didn't pay attention to the first time. So for the actual printing at each level (the part other than the recursion), you need h tabs to the left of the text.
 
Old 05-03-2013, 12:03 PM   #9
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
@johnsfine

No, i did not write the code on my own. i did try n replace "n" with "h", still gives me a warning
 
Old 05-03-2013, 12:06 PM   #10
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
@mlslk31

the allocation is made elsewhere, and yes it dint make sense to me either:/
 
Old 05-03-2013, 12:48 PM   #11
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by lxmirmsh View Post
No, i did not write the code on my own. i did try n replace "n" with "h", still gives me a warning
That was supposed to be change just the n in i<n to h. Any other n was probably correct.

I don't see how that could give the same warning. Maybe you forgot to save the edit and recompiled the earlier version again.

What line is the warning on (and if it is a different warning, what is the warning)?

Last edited by johnsfine; 05-03-2013 at 12:51 PM.
 
Old 05-04-2013, 11:14 AM   #12
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
yeah I did change the "n" to "h" only in i<n and no, it does not give the same warning.
when I replace n with h, I get this - "warning: assignment from incompatible pointer type [enabled by default] "
 
Old 05-05-2013, 06:27 AM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
Quote:
Originally Posted by lxmirmsh View Post
yeah I did change the "n" to "h" only in i<n and no, it does not give the same warning.
So you fixed that problem and you have some other problem.

Quote:
I get this - "warning: assignment from incompatible pointer type [enabled by default] "
A warning message like that includes the line number of the source line that caused the warning.

For someone to help you, you must post source code including the line the warning complained about, and you must identify which line it was.
 
Old 06-03-2013, 12:38 PM   #14
lxmirmsh
LQ Newbie
 
Registered: Nov 2012
Location: India
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thank you all for your help!
The warning is removed. yes, all i had to do was to replace "n" with "h". When i replaced it last time, I'd made a typing mistake elsewhere. Thats's why i got a warning.
 
  


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
warning: comparison between pointer and integer Mr Oblivion Programming 3 02-26-2013 09:13 AM
[SOLVED] comparison between pointer and integer texasone Programming 14 07-10-2010 08:42 AM
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 10:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration