Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Easy C++ question

Posted on 2007-11-20
3
Medium Priority
?
249 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 200 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:Deepu Abraham
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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