LinuxQuestions.org

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

Linh 06-06-2003 12:56 PM

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.

0x4B 06-06-2003 02:39 PM

" " and ' ' are not the same. use single quotes to represent a character, otherwise its an array of characters (which is a pointer)

Linh 06-06-2003 03:14 PM

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.

0x4B 06-06-2003 03:47 PM

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)

0x4B 06-06-2003 03:49 PM

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>


All times are GMT -5. The time now is 07:42 AM.