Solved

Easy C++ question

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now