i get a string str in a signature
and i dont have an index=0 to use
i cant use strlen because its a function it will give 1 even if the function has 100 cells
i need to do it in one line
len=??1??
i get a string str in a signature
and i dont have an index=0 to use
i cant use strlen because its a function it will give 1 even if the function has 100 cells
i need to do it in one line
len=??1??
Please show what related code you have already, and please please please try to clarify the issue you are having. Your fragmented question provides literally no context to understand your problem.
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
Code:what(char *str) { int len; len=?? }
Ok so why doesn't this work?
http://www.cplusplus.com/reference/c...ng/strlen.htmlCode:len = strlen(str);
Environment: OS X, GCC / G++
Codes: Java, C#, C/C++
AOL IM: neandrake, Email: neandrake (at) gmail (dot) com
If strlen gives 1 for a char[100], it's because the second character is the null terminator ('\0'). strlen tells the number of characters which precede the null terminator, not how many bytes are allocated to a variable.
For example:That will print:Code:int main(int agrc, char **argv){ char *foo = "ab\0def"; printf("The string \"%s\" is %i characters long.", foo, strlen(foo)); }The string "ab" is 2 characters long.
because its in external function and we have our array decays to one cell
What?
"If you tell the truth, you don't have to remember anything"
-Mark Twain
Sounds like you're trying to implement something to mimic decay mechanism/function of some sort. Might help if you print the block of code that's implementing your logic as I suspect that's where you're going wrong.
A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside
how to write so it will workCode:what(char *str) { int len; len = sizeof(str)/sizeof('a'); // Won't work! }
??
Last edited by transgalactic2; 03-24-2009 at 01:41 AM.
i tried
it doesnt workCode:#include <stdio.h> #include <string.h> void what(char* str); int main() { char arr[5]={'a','b','c','d','e'}; what(arr); return 0; } void what(char* str) { int len; len=strlen(str); printf("%d",len); }
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd
i changed it it works but i get this warningCode:#include <stdio.h> #include <string.h> void what(char* str); int main() { char arr[6]={'a','b','c','d','e','\0'}; what(arr); return 0; } void what(char* str) { int len; len=strlen(str); printf("%d",len); }
c(14) : warning C4267: '=' : conversion from 'size_t' to 'int'
what is size_t why i get this warning
when I run your code, the version with:I get the output 7Code:char arr[5]={'a','b','c','d','e'};
but with the null terminated one I get the correct output.
What compiler are you using?
I used dev C++.
Last edited by WDT; 03-24-2009 at 03:13 AM.
A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside
Stop reposting your questions! It's annoying.
As to size_t, it's a datatype. In this case strlen returns size_t (which is probably an unsigned int or something). You declared len as an (signed) int.
to make the program correct, you should either use unsigned int (since strlen cannot return a -ve number) or use size_t len;
QuantumPete
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd