Solved

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

Posted on 2003-11-10
3
377 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 153
How to split this in C++ 4 119
Precision Problem in C++ 7 50
How to increment counter variables in ANSI  C (Not C# or C++). 9 42
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

749 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