Solved

Converting a string to a character array (char[])

Posted on 2003-11-10
3
380 Views
Last Modified: 2010-04-01
suppose I have

string arg6; //my 6th argument for some function to be called soon

however i have to pass in this argument as a character array, char[]

I tried,

char x[] = arg6.c_str();

but i get some const char* cannot be converted to char[] error...
is there a way to do what I want? I solved my problem by declaring arg6 as a char[] but i still want to know how to convert a string to char[] for my own sake. I do know how to conver it to a char*.

thanks
0
Comment
Question by:lapchern
[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 9

Accepted Solution

by:
tinchos earned 50 total points
ID: 9719754
Hi lapchern,

try with

char x[256];
char *p = x;

strncpy( p, arg6.c_str(), 256 );


Cheers!

Tincho
0
 

Author Comment

by:lapchern
ID: 9719782
cheers!
0
 
LVL 4

Expert Comment

by:skypalae
ID: 9720295
Hi,

The error you mentioned is that compiler cannot convert pointers to arrays (although they are almost the same, it is question of the array size i suppose). So why don't you write your code like this?

string s = "abcd" ;
char *c = (char *) s.c_str () ; // can be 'const char *c = s.c_str ()'

This works ok for me (using VC++ 6.0) ;

S.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

690 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