Solved

Easy C++ question

Posted on 2007-11-20
3
244 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

786 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