Solved

Easy C++ question

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 131
How to Correctly derive class from CWinThread in MFC 4 116
FMX TEdit KeyUp handler detecting  "enter" key 4 63
learn programming 8 94
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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…
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 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.

734 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