How to convert "string" type to "char * " in c++
I have been trying to write some codes on certain project that I am working on. I am not able to convert the datatype "string" to "char * " by using simple type cast,
Code:
string l="Pranav"; 2. Is it possible to access the characters of a "string" in C++ one by one? PS: I am using "g++" on my FD3 operating system. |
Quote:
|
In your sample let 'p' point to the address of the first char of string 'l':
Code:
string l="Pranav"; Code:
string l="Pranav"; |
how to find the number of elements in a string array?
Quote:
i have another problem though... :newbie: i have a program that accepts an array of string int func(string pieces[ ]) how do i know how many elements in the array have been passed?? As far as i know "pieces[0].length()" gives the number of characters in a string. however "pieces.length()" doesn't work. plz help... PS: I am not supposed to use an explicit argument for the number of values passed. |
Quote:
Code:
int func(string *pieces) Code:
int func(string *pieces, unsinged int length) http://hea-www.harvard.edu/MST/simul...oc/Vector.html |
i was hoping for an easier solution
too complex... there has to be a way around :scratch:
thanx a lot ------------------------------------------------------------------------ And they say that a hero can save us, i'm not going to stand here and wait |
Quote:
|
It really isn't very complex, here I worte a little program for you. All it does is building a string vector ("strings") and pass this to a function that will dump it.
Code:
#include <string> |
Quote:
Problem Statement In this problem we are going to make an 11x11 chess board out of the Cartesian plane. The positions on our board will be the coordinates (x,y) where 0 <= x <= 10, 0 <= y <= 10, and x,y are integers. We will consider two different chess pieces: rooks and bishops. A rook may move either horizontally or vertically. It may move any number of spaces in a straight line, so long as it does not run into another piece on the board. Each location that the rook can move to is considered 'under control'. A bishop is similar, except that it moves diagonally, instead of horizontally and vertically. For example, the following diagram shows the 11x11 board with one rook ('R') and one bishop ('B') where the locations under control are denoted by 'X': Code:
. . X . . . . X . . . Definition Class: UnderControl Method: howMany Parameters: String[] Returns: int Method signature: int howMany(String[] pieces) <- See why i dont want to use vector (be sure your method is public) Constraints - pieces will contain between 1 and 50 elements inclusive. - Each element of pieces will have the form (quotes for clarity) "x y P" where: x is an integer with no extra leading zeros between 0 and 10 inclusive, y is an integer with no extra leading zeros between 0 and 10 inclusive, and P is either Rook or Bishop. - No two pieces will be at the same location. Examples 0){"0 0 Rook"} Returns: 20 A rook in the lower left corner controls 20 squares. Each controlled square has the form (0,y) or (x,0) - with the exception of (0,0), which is the position of the rook. 1) {"0 0 Bishop"} Returns: 10 A bishop in the lower left corner only controls 10 squares. 2) {"5 5 Bishop"} Returns: 20 Near the center, the bishop controls more squares. 3) {"0 5 Rook","2 5 Rook","5 5 Rook","9 5 Rook"} Returns: 47 Many rooks sharing the same y-coordinate. 4) {"0 0 Bishop","1 1 Rook"} Returns: 20 The rook blocks the path of the Bishop. 5) {"2 5 Bishop","2 8 Rook"} Returns: 27 So what shud i do? Heres my code, which doesn't compile (for the reasons Flesym pointed out), Code:
#include<iostream> |
Ok, I haven't read the whole thing because I had a very long night with lots of beer and aren't able to understand anything today, but as I already said, you cannot pass an array to a function, but only the pointer to its first element! So you have to declare a second parameter with the size, or you use a struct or class that manages the data and size for you. Sorry, but there is no workaround! But as far as I can see, you can do it exactly like I showed you in my last post:
First define your own type, a vector of strings. This is not really necessary but it make things better readable: Code:
typedef vector<string> vec2str; Code:
class UnderControl{ Code:
int main() Oh and one more general tip: In case, that in the original code the "UnderControl class" is declared in a header file (you should do this)then don't use the "using namespace" there! Have a look here: http://nepsweb.co.uk/pgtcpp/namespace.htm |
Not sure why you'd want to change it into a C string, when there's a function to return the C string part of a string. Then you MIGHT be able to do something like this.
Code:
char *p; I think that's it, if I remember correctly. :) |
You have to allocate memory for the string first
Code:
char *p=new char[l.size()]; |
why didn't i think of it..
Quote:
thanx that works... the vetors needn't be passed as args... whyu didn't i thought of it earlier... |
This is me jumping on the bandwagon.
Can anyone explain to me why this doesn't compile? I can't figure it out for the life of me. Code:
string mystring="testing"; Code:
error: invalid conversion from ‘const char*’ to ‘char*’ Thanks, Jon |
c_str() returns a constant C-string. I suppose you could use strcpy() or strncpy() (cstring header) to do what you want.
|
All times are GMT -5. The time now is 12:28 AM. |