Passing valid string through StrLen()

This is a discussion on Passing valid string through StrLen() within the C Programming forums, part of the General Programming Boards category; Howdy! I am reviewing for my final and one of the questions states that Code: StrLen(c); is not a valid ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    10

    Passing valid string through StrLen()

    Howdy! I am reviewing for my final and one of the questions states that
    Code:
    StrLen(c);
    is not a valid function call when
    Code:
    char c = 'c';

    Can someone help me understand why?



    Examples of valid function calls in the review are

    Code:
    StrLen(cp); 
    StrLen(ca);
    StrLen("abc");
    when

    Code:
    char *cp = "hello";
    char ca[] = "world";

    It's my first post here, and I apologize for the sloppy formatting.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You give the answer in your post title. You should only pass strings to strlen, and c is not a string.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You give the answer in your post title. You should only pass strings to strlen, and c is not a string.
    I just realized that one character can't be a string...thank you

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Technically, one character can be a string. It would be a string containing only the null character.
    Code:
    char null_str[] = "";
    The reason why your example is not a valid function call is because strlen() takes as its argument a pointer to a string, and string is defined in C as a sequence of character values, terminated by a null character.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    10
    There you go, that's what I was looking for. Strings are null terminated! Thanks =))))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble passing a string into strlen()...
    By yaya in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2008, 04:46 AM
  2. recursion error
    By cchallenged in forum C Programming
    Replies: 2
    Last Post: 12-18-2006, 09:15 AM
  3. Passing an 8-bit String
    By slowcoder in forum C Programming
    Replies: 8
    Last Post: 08-01-2006, 08:18 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21