473,394 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Getting problem while executing strlen() in C++

when i am executing cout<<strlen("avinash"); in the main function, i am getting output as 7....but whereas if i execute as following

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string a = "avinash";
cout<<strlen(a);
}

Now, i am getting an error that cannot covert std::string ot const chat* for argument 1 to size_t strlen(const char*)

but if do using string length, i am getting the perfect answer

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string a = "avinash";
cout<<a.length();
}

Now i am getting the correct output as 7

can u please tell me why i am unable to get the same output using cout<<strlen(a);
Oct 26 '14 #1
1 1121
weaknessforcats
9,208 Expert Mod 8TB
You cannot use strlen on a C++ string object. strlen assumes a null-terminated char array. A sring object my not have that structure.

You are supposed to use the size() method of string.

You can, however, tell the string object to produce a C-style null terminated string by using the c_str() method:

Expand|Select|Wrap|Line Numbers
  1.  cout<<strlen(a.c_str()); 
Oct 26 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: pvssvikas | last post by:
Good Afternoon, I'am badly in need of help. My task :--- I would like to create a node set of elements through C++ code while executing a style sheet and process the created node set. Node...
8
by: pratha | last post by:
Hi, i have a problem received from my client saying that she received an error that say : An exception occurred while executing a Transact-SQL statement or batch. There is no error code given in...
0
by: hi4murali | last post by:
Facing a strange error in MS SQL Management Studio 2005, when trying to open a new query window, open a table to view data or even run and existing SQL query file. The error text is: An error...
3
by: mdfidahussain | last post by:
Hello VB Experts, I've encountered an error -> "SWbemServicesEx: Quota Validation" while executing a VB Script, infact the script is simple and is also running fine for many servers but only...
1
by: mahesh123 | last post by:
Hi, While executing my asp.net Project i am getting the following error. It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This...
2
by: gaurav kashyap | last post by:
HI all, i have two python programs as 1.py and 2.py 1.py import os import sys processID=os.spawnl(os.P_WAIT,'/usr/local/bin/python','python','/ mywork/2.py ' + 'hi') 2.py
8
by: chansra agnuru | last post by:
Hi, I need some information regarding how to handle scanf values while executing the program itself. For example: main(int argc, char* argv) { char str; printf("first argument: %s",__argv);...
3
by: vvtt | last post by:
Iam new to PHP, I have experience in C/C++. I have the following doubt: In C or C++ 1) function variables are stored on stack. 2) Global and Static variables are stored on data segment(.bss...
0
by: exp123 | last post by:
I am executing a store procedure on V9.7. When i run the SP using JDBC call am getting below error, " SQL0440N No authorized routine named "PROCEDURE" of type "XXX" having compatible arguments...
0
by: ganesh3691 | last post by:
hi.....i have integrated a new protocol to ns2...the object files for the protocol is sucessfully created. but while running the tcl script im getting a error that is given below...any one pls help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.