Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-11-10
3
Medium Priority
?
389 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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