LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 06-06-2003, 12:56 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
C programming error. warning: comparison between pointer and integer


1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>

4 int main()
5 {
6 char string1[] = "ifconfig";
7 FILE *f;

8 int i = 0;
9 char single_char;
10 char *word[30];

11 f=popen("ifconfig", "r");
12 while(!feof(f))
13 {
14 single_char = fgetc(f);
15 if (single_char != " ")
16 {
17 *word[i] = single_char; /*make a word */
18 i++;
19 }
20 else
21 {
22 i = 0; /*a word has ended, so reset the counter */
23 printf("%s", &word);
24 }
25 }
26 pclose(f);
27 return(0);
28 }
===========================================

I got an error when compile and the message is:
test3.c:15: warning: comparison between pointer and integer
It is complaining on line 15.
if (single_char != " ")

On the above code, I wan to know if a character is a space or not a space.
 
Old 06-06-2003, 02:39 PM   #2
0x4B
Member
 
Registered: May 2003
Location: Nashville TN, USA
Distribution: Debian (I'm unstable)
Posts: 117

Rep: Reputation: 15
" " and ' ' are not the same. use single quotes to represent a character, otherwise its an array of characters (which is a pointer)
 
Old 06-06-2003, 03:14 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
C program error

I did change it from double quote to single quote.

if (single_char != ' ')

It compiled fine. When I ran it (./test3), it said Segmentation fault.
 
Old 06-06-2003, 03:47 PM   #4
0x4B
Member
 
Registered: May 2003
Location: Nashville TN, USA
Distribution: Debian (I'm unstable)
Posts: 117

Rep: Reputation: 15
you need to make sure that you don't try to access anything past word[29]. A segmentation faul is when you try to access memory that you shouldn't, and is a common problem with pointers. (I didn't look too carefully, there could be other places that it would seg fault too)
 
Old 06-06-2003, 03:49 PM   #5
0x4B
Member
 
Registered: May 2003
Location: Nashville TN, USA
Distribution: Debian (I'm unstable)
Posts: 117

Rep: Reputation: 15
oh, char *word[30] is probably not what you want, if you're thats an array of 30 char pointers (and you're not allocating any space to use any of them, which is why its seg faulting)

<edit>

I've confirmed that with
&& i < 30
in the while statement
and by removing the * from both instanaces of word it will run to completion.

</edit>

Last edited by 0x4B; 06-06-2003 at 03:51 PM.
 
  


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
Comparison between pointer and integer ---> WHY?? its_godzilla Programming 10 01-28-2005 09:40 PM
makes pointer from integer without a cast ? hubabuba Programming 2 01-28-2005 05:28 PM
pointer from integer without a cast bcf2 Programming 7 12-30-2004 02:04 PM
ISO C++ forbids comparison between pointer and integer? pimaster Programming 1 11-06-2003 01:45 PM
pointer to bog standard integer acid_kewpie Programming 4 02-02-2002 03:14 PM


All times are GMT -5. The time now is 12:14 AM.

Main Menu
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