Warning: Comparison between pointer and integer (mingw 5.1.4)

Thread Solved

Join Date: Jul 2008
Posts: 6
Reputation: java_girl is an unknown quantity at this point 
Solved Threads: 0
java_girl java_girl is offline Offline
Newbie Poster

Warning: Comparison between pointer and integer (mingw 5.1.4)

 
0
  #1
Dec 2nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Warning: Comparison between pointer and integer (mingw 5.1.4)

 
0
  #2
Dec 2nd, 2008
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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char* longer( char str1[], char str2[] );
  6. char* str_ptr;
  7.  
  8. int main( void ) {
  9.  
  10. char str1[] = "I like cheese but I dislike brussel sprouts!";
  11. char str2[] = "I like cheese and pasta!";
  12.  
  13. str_ptr = longer( str1, str2 );
  14.  
  15. printf( "The longer string is: %s\n", str_ptr );
  16.  
  17. return 0;
  18. }
  19.  
  20. char* longer( char str1[], char str2[] ) {
  21.  
  22. int i = 0, count1 = 0, count2 = 0;
  23.  
  24. count1 = strlen(str1);
  25. count2 = strlen(str2)
  26.  
  27.  
  28. if( count1 == count2 ) {
  29. puts( "The strings are of equal length!" );
  30. return "";
  31. } else {
  32. if ( count1 > count2 ) {
  33. return str1;
  34. } else {
  35. return str2;
  36. }
  37. }
  38. }

And if you want to calculate the length manually then in while-loop
put this condition:

  1. int count = 0;
  2. while(str1[count] != '\0')
  3. count++
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Warning: Comparison between pointer and integer (mingw 5.1.4)

 
0
  #3
Dec 2nd, 2008
Wrong using of a POINTER constant defined as NULL macros in C:
  1. while( str1[i] != NULL) /* wrong */
  2. /* Right code: */
  3. while (str1[i]) /* (I prefer this form); or */
  4. while (str[i] != '\0') /* or */
  5. while (str[i] != 0)
The C compiler can define NULL as (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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Warning: Comparison between pointer and integer (mingw 5.1.4)

 
0
  #4
Dec 2nd, 2008
Correct way of using code tags:

[code=c]
.
.
Your code here
.
.
[/code]
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Warning: Comparison between pointer and integer (mingw 5.1.4)

 
0
  #5
Dec 2nd, 2008
rather:

[code='c']
Last edited by Luckychap; Dec 2nd, 2008 at 7:59 am.
When you think you have done a lot, then be ready for YOUR downfall.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Warning: Comparison between pointer and integer (mingw 5.1.4)

 
0
  #6
Dec 2nd, 2008
Posts can be edited for upto 15 minutes after they are created(someone correct me if I'm wrong). After that you'll just have to start a new post.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC