cuziyq
asked on
Easy C++ question
If you have a function that takes a char* as one of its arguments, and then call that function passing in a string, will it be null terminated? If not, how do you get the length of the string?
For example, if your function looks like this:
void Foo(char *bar)
{
int i=strlen(bar);
}
...and you called it like this:
Foo("Hello, World") //Note the lack of an explicit null terminator
Would strlen() in the Foo function get an accurate count since it requires a null terminator? If not, how would I be able to get the count?
For example, if your function looks like this:
void Foo(char *bar)
{
int i=strlen(bar);
}
...and you called it like this:
Foo("Hello, World") //Note the lack of an explicit null terminator
Would strlen() in the Foo function get an accurate count since it requires a null terminator? If not, how would I be able to get the count?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> but it is dangerous to pass like the way you have mentioned.
or :
void Foo(const char *bar) {
int i = strlen(bar);
}
Foo("Hello, World");
or :
void Foo(const char *bar) {
int i = strlen(bar);
}
Foo("Hello, World");
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main( void )
{
char buffer[61] = "How long am I?";
int len;
len = strlen( buffer );
printf( "'%s' is %d characters long\n", buffer, len );
}
//------------------------
you can do like this
void Foo(char *bar)
{
char buff[MAX_BUFF]; //if you know the buffer length #define MAX_BUFF 100
if(bar)
strcpy(buff,bar);
int i=strlen(bar);
}
Best Regards,
DeepuAbrahamK