| | |
Warning: Comparison between pointer and integer (mingw 5.1.4)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jul 2008
Posts: 6
Reputation:
Solved Threads: 0
Hi.
This is NOT a homework assignment (self-interest only).
The exercise asks me to write a function which accepts two strings and returns a pointer to the longer of the two.
[code language="C"]
#include <stdio.h>
#include <stdlib.h>
char* longer( char str1[], char str2[] );
char* str_ptr;
int main( void ) {
char str1[] = "I like cheese but I dislike brussel sprouts!";
char str2[] = "I like cheese and pasta!";
str_ptr = longer( str1, str2 );
printf( "The longer string is: %s\n", str_ptr );
return 0;
}
char* longer( char str1[], char str2[] ) {
int i = 0, count1 = 0, count2 = 0;
while( str1[i] != NULL ) {
i++;
count1++;
}
while( str2[i] != NULL ) {
i++;
count2++;
}
if( count1 < count2 )
return str2;
else if ( count1 > count2 )
return str1;
else
puts( "The strings are of equal length!" );
}
[/code]
The compiler returns the following warnings:
ex106.c: In function `longer':
ex106.c:23: warning: comparison between pointer and integer
ex106.c:28: warning: comparison between pointer and integer
I'm unsure of how exactly to resolve the problems but am I correct in assuming it is a result of using an index on the str1 and str2 arrays?
Thanks,
java_girl
This is NOT a homework assignment (self-interest only).
The exercise asks me to write a function which accepts two strings and returns a pointer to the longer of the two.
[code language="C"]
#include <stdio.h>
#include <stdlib.h>
char* longer( char str1[], char str2[] );
char* str_ptr;
int main( void ) {
char str1[] = "I like cheese but I dislike brussel sprouts!";
char str2[] = "I like cheese and pasta!";
str_ptr = longer( str1, str2 );
printf( "The longer string is: %s\n", str_ptr );
return 0;
}
char* longer( char str1[], char str2[] ) {
int i = 0, count1 = 0, count2 = 0;
while( str1[i] != NULL ) {
i++;
count1++;
}
while( str2[i] != NULL ) {
i++;
count2++;
}
if( count1 < count2 )
return str2;
else if ( count1 > count2 )
return str1;
else
puts( "The strings are of equal length!" );
}
[/code]
The compiler returns the following warnings:
ex106.c: In function `longer':
ex106.c:23: warning: comparison between pointer and integer
ex106.c:28: warning: comparison between pointer and integer
I'm unsure of how exactly to resolve the problems but am I correct in assuming it is a result of using an index on the str1 and str2 arrays?
Thanks,
java_girl
there is a simple function to calculate a string length strlen(str) defined in string.h where str is the pointer to string of which you want to calculate the length.
And if you want to calculate the length manually then in while-loop
put this condition:
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> char* longer( char str1[], char str2[] ); char* str_ptr; int main( void ) { char str1[] = "I like cheese but I dislike brussel sprouts!"; char str2[] = "I like cheese and pasta!"; str_ptr = longer( str1, str2 ); printf( "The longer string is: %s\n", str_ptr ); return 0; } char* longer( char str1[], char str2[] ) { int i = 0, count1 = 0, count2 = 0; count1 = strlen(str1); count2 = strlen(str2) if( count1 == count2 ) { puts( "The strings are of equal length!" ); return ""; } else { if ( count1 > count2 ) { return str1; } else { return str2; } } }
And if you want to calculate the length manually then in while-loop
put this condition:
c Syntax (Toggle Plain Text)
int count = 0; while(str1[count] != '\0') count++ printf("\nLength of str1 = %d", count);
Last edited by Luckychap; Dec 2nd, 2008 at 3:15 am.
When you think you have done a lot, then be ready for YOUR downfall.
Wrong using of a POINTER constant defined as NULL macros in C:
The C compiler can define NULL as
Moral: NULL is not a zero alias in C, use NULL only for pointer values.
c Syntax (Toggle Plain Text)
while( str1[i] != NULL) /* wrong */ /* Right code: */ while (str1[i]) /* (I prefer this form); or */ while (str[i] != '\0') /* or */ while (str[i] != 0)
(void*)0 or simple 0. In the 1st case you are trying to compare char promoted to int with a pointer to void.Moral: NULL is not a zero alias in C, use NULL only for pointer values.
Last edited by ArkM; Dec 2nd, 2008 at 6:04 am.
![]() |
Other Threads in the C Forum
- Previous Thread: Trouble with pointers
- Next Thread: /etc/resolv.conf
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework ide include initialization input interest intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






