Thread: Calculating the length of a string

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Calculating the length of a string

    I am trying to calculate the length of a string and finding the compiler aren't getting along.

    For instance "code " == 4 I don't want it to include the null character but I am missing something. I thought a cast was when you have an int for instance and you want to return a value. For instance, you want to go from a conversion specifier of better precision and you have declared intengers.

    Code:
     #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 int stringlength( const char *s);
      7 int count = 0;
      8 
      9 int main (void)
     10 
     11 {
     12 
     13 char s [ 50 ];
     14 int length;
     15 
     16 printf("Please enter a string\n");
     17 scanf("%s", s);
     18 
     19 length = stringlength(count);
     20 
     21 printf("The length of the string is %d\n",length);
     22 
     23 return 0;
     24 
     25 }
     26 
     27 int stringlength( const char *s )
     28 
     29 {
     30 
     31 
     32 while (  *s != NULL ) {
     33 count++;
     34 }
     35 
     36 return count;
     37 }
    
    
    
    
    warning: passing argument 1 of āstringlengthā makes pointer from integer without a cast
    
    In function āstringlengthā:
    :32: warning: comparison between pointer and inte

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I have declared count globally or atleast I think I have. Why can't I pass in the array s and return count. It makes sense to me.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    look closely.
    Code:
      6 int stringlength( const char *s);
      7 int count = 0;
    
     19 length = stringlength(count);

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    line 6 I am passing in const char *s. I don't want the function to modify the string.

    I have declared the global variable to 0. I can't increment it once I have declared it to be 0 in the while statement.

    I want to return count to the function stringlength because that's what's counting my string.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I am returning an intenger to my function.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're not catching it. Look again. What are you actually passing to stringlength()?

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    The count. If I pass it back the array. How does it know how many characters are in the array?

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    ...... Um...... Apparently you have to study functions again.

    You declared stringlength() as taking a const char *, but you're passing it an int. Do you not see this as wrong? If not, you made a mistake somewhere in your understanding of how this works.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    My book says to use const in being careful not to allow the function to modify the array or pointer.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is irrelevant.
    You are passing an int, while the function is expecting const char*.
    int != const char*.
    The end.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Once you figure out which parameter you should be passing to your stringlength() function (which is just duplicating the standard strlen() function) you need to fix the bug in stringlength() (or just use strlen() instead).
    HINT: You have an infinite loop.

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    How do I have an infinite loop. The while should exit when it reaches the null character. Setting the count to however many times it counted up to the null character.

    Code:
     #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 int stringlength(  char s, 50);
      7 int count = 0;
      8 
      9 int main (void)
     10 
     11 {
     12 
     13 char s [ 50 ];
     14 int length;
     15 
     16 printf("Please enter a string\n");
     17 scanf("&#37;s", s);
     18 
     19 length = stringlength(count);
     20 
     21 printf("The length of the string is %d\n",length);
     22 
     23 return 0;
     24 
     25 }
     26 
     27 int stringlength(  char s, 50 )
     28 
     29 {
     30 
     31 
     32 while (  s != NULL ) {
    count++;
     34 }
     35 
     36 return count;
     37 
     38 
     39 }
    
     error: expected declaration specifiers or &#226;...&#226; before numeric constant
    .c:27: error: expected declaration specifiers or &#226;...&#226; before numeric constant
     In function &#226;stringlength&#226;:
     warning: comparison between pointer and integer

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Seriously, stop using C. It's obvious you have no idea what you're doing.
    Go back to your books some weeks and try again afterwards.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I wish I could. lol

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Sorry if I offended anyone. I am really trying to learn this and someone states I have an infinite loop. I state what "I think it is doing" and ask a question and Elysia gets ........ed off. What the heck?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM