strlen problem

This is a discussion on strlen problem within the C++ Programming forums, part of the General Programming Boards category; i have this lib: #include <cstring.h> Code: string message; length = strlen(message); why wont it work....

  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, 09:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07: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, 09:37 AM

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