Solved

Easy C++ question

Posted on 2007-11-20
3
247 Views
Last Modified: 2010-04-01
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?
0
Comment
Question by:cuziyq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 20

Accepted Solution

by:
ikework earned 50 total points
ID: 20320264
>> Foo("Hello, World")     //Note the lack of an explicit null terminator

the compiler adds a terminating zero-byte to string-constants, so there is an termination zero-byte and strlen will work ..

ike
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 20320300
strlen() checks for null termination and gets the length, However the method you have written may succeed but it is dangerous to pass like the way you have mentioned.

#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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20320516
>> 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");
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question