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?
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)
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.
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)
%s is two characters.
Post some code because you aren't being very clear.
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.
Is this close to what you are trying to find, GaPe?[edits][more edits][yet another edit, subsequent posts deleted]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 */
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.*
The reason for getting: strlen("hello, my name is %s") = 20Originally posted by Dave_Sinkula
Is this close?[edits]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 */
strlen("Dave Sinkula") = 12
Is because your printf functionhas 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.Code:printf(strlen(\"%s\") = %lu\n", fmt, (long unsigned)strlen(fmt));
If the output shown is what you intended, then WTF is the problem?Originally posted by Dave_Sinkula
??? Did you try this? It is not even valid syntax.
The output shown is what I intended.
Maybe I did RTFT. You are not making any sense of what you want.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).
If you wish to continue an argument w/ this, PM me
Thank you Dave_Sinkula.
Current projects:
1) User Interface Development Kit (C++)
2) HTML SDK (C++)
3) Classes (C++)
4) INI Editor (Delphi)
>>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]