Thread: Size of the string

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Size of the string

    Hi!

    I have a function "void Message (char *Message, ...);". Now I want to know the size of the string Message in that the same function. How do I do that?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    If it is null terminated strlen() in string.h will tell you the length. Otherwise you'll have to pass the length to the function manually.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Eh, maybe I wasn't clear enough. Look I know the strlen function, but how do you determine the length of the string if it is declared as "hello, my name is %s". How do you know the length of %s???
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    %s is two characters.

    Post some code because you aren't being very clear.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    If I understand what do you want, you can print that string to a string using sprintf, which will print also substitution for %s and then use strlen() on the result.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Is this close to what you are trying to find, GaPe?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>
    
    void Message(const char *fmt, ...)
    {
       va_list  args;
       char     *arg;
       int      length;
    
       va_start(args, fmt);
       length = vprintf(fmt, args);
       arg = va_arg(args, char*);
       va_end(args);
    
       printf("\nlength (with substitution) = %d\n", length);
       printf("strlen(\"%s\") = %lu\n", fmt, (long unsigned)strlen(fmt));
       printf("strlen(\"%s\") = %lu\n", arg, (long unsigned)strlen(arg));
    }
    
    int main(void)
    {
       Message("hello, my name is %s", "Dave Sinkula");
       return 0;
    }
    
    /* my output
    hello, my name is Dave Sinkula
    length (with substitution) = 30
    strlen("hello, my name is %s") = 20
    strlen("Dave Sinkula") = 12
    */
    [edits][more edits][yet another edit, subsequent posts deleted]
    Last edited by Dave_Sinkula; 09-14-2003 at 12:21 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Dave_Sinkula
    Is this close?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>
    
    void Message(const char *fmt, ...)
    {
       va_list  args;
       char     *arg;
    
       va_start(args, fmt);
       arg = va_arg(args, char*);
       va_end(args);
    
       printf("strlen(\"%s\") = %lu\n", fmt, (long unsigned)strlen(fmt));
       printf("strlen(\"%s\") = %lu\n", arg, (long unsigned)strlen(arg));
    }
    
    int main(void)
    {
       Message("hello, my name is %s", "Dave Sinkula");
       return 0;
    }
    
    /* my output
    strlen("hello, my name is %s") = 20
    strlen("Dave Sinkula") = 12
    */
    [edits]
    The reason for getting: strlen("hello, my name is %s") = 20
    strlen("Dave Sinkula") = 12

    Is because your printf function
    Code:
    printf("strlen(\"%s\") = %lu\n", fmt, (long unsigned)strlen(fmt));
    has strlen(\"%s\") inside of quotes. If you want to display it correctly, replace your printf code to this:
    Code:
    printf(strlen(\"%s\") = %lu\n", fmt, (long unsigned)strlen(fmt));
    Because in printf, if u do this: printf("strlen(hi)"); it'll output strlen(hi), but if yo uuse printf(strlen(hi)); you'll get the string length.

  8. #8
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Dave_Sinkula
    ??? Did you try this? It is not even valid syntax.

    The output shown is what I intended.
    If the output shown is what you intended, then WTF is the problem?

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Dave_Sinkula
    >If the output shown is what you intended, then WTF is the problem?

    I am not the OP asking the question. Read the whole thread (or maybe, RTFT).
    Maybe I did RTFT. You are not making any sense of what you want.

    If you wish to continue an argument w/ this, PM me

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thank you Dave_Sinkula.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but if yo uuse printf(strlen(hi)); you'll get the string length.
    No you won't That snippet of code is invalid.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM