Thread: strlen problem

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    strlen problem

    i have this lib: #include <cstring.h>



    Code:
    string message;
    length = strlen(message);
    why wont it work.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) length isn't defined any place.
    2) message doesn't appear to be a valid string.
    3) strlen uses character pointers, and 'message' is a class.*

    *I might be off here, becuase they may have a different version of strlen in C++, but I don't believe so.

    In short, your code is incomplete for me to be sure of exactly where your problem lies, but I believe #2 is your main problem.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    It won't work because you're using a c-style string function on a C++ string class. Either use char *message or call string::length() to retrieve the length.

    For example...
    string message = "Hello"
    string::size_type length = message.length();

    or

    char *message = "Hello";
    size_t length = strlen(message);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM