Hi ppl,

I have made a declaration:

char* str;

str = " Hi there";

what do i have to do to extract a substring from this?

Thanks,
Ishwar

Use the strstr function perhaps? I'm assuming you're using C, because you'd be better of using the std::string class and substr functions if you're using C++.

I am using C++,

I tried using the string class,
like this :

char * str;
string str2 = "hello";
 
str = str2.substr(2,4);
 
// doesnt work, my aim is to get substr in a char*

Ishwar

char * str;
  string str2 = "hello";
  str = const_cast<char*>((str2.substr(2,4)).c_str());

But why don't you use string class completely like this

string str;
  string str2 = "hello";
  str = str2.substr(2,4);

std::string.substr() returns another std::string.and you can't mix C and C++ like that. you would need to allocate space for the substring

char * str;
string str2 = "hello";
string str3; 
str3 = str2.substr(2,4);
str = new char[str3.length()+1];
strcpy(str,str3.c_str());

C isn't nearly as kind to you as c++.

std::string.substr() returns another std::string.and you can't mix C and C++ like that. you would need to allocate space for the substring

char * str = NULL;
string str2 = "hello";
string str3; 
str = (str2.substr(2,4)).c_str();

Mr. Dragon, wont this work or do the same trick or the const nature of the returned null terminated char array pose some problems ?

~s.o.s~
You will need a cast.

~s.o.s~
You will need a cast.

So should i use the <const_cast> ?

Yes, you should see my example.

commented: Good one : ~s.o.s~ +1

Mr. Dragon, wont this work or do the same trick or the const nature of the returned null terminated char array pose some problems ?

It will compile but displays garbage at runtime because the string returned by substr() is only temporary object which must be copied someplace else such as another std::string object or char array.

#include <string>
#include <iostream>
using namespace std;

int main()
{

	string str = "Hello World";
	const char *p = str.substr(2,4).c_str();
	cout << p << endl;
	return 0;
}

Thanks ppl,

It works now,

I type casted the str2.substr().


Ishwar

> It works now,
You mean
"It works for now"

> I type casted the str2.substr().
If you needed a cast to get out of such a small problem, then you've almost certainly done the wrong thing and you're in for problems later on.

> It works now,
You mean
"It works for now"

> I type casted the str2.substr().
If you needed a cast to get out of such a small problem, then you've almost certainly done the wrong thing and you're in for problems later on.

what do you mean?

what do you mean?

I guess you didn't read my previous post. casting only makes it compile -- that doesn't mean it will actualy work. Did you test your program, something similar to what I posted? If not, then you are in for a big supprise.

A better question is why you're still so desperate to cling to a char* when you're using C++ strings.

c_str() returns a const char * for a reason, that reason being you shouldn't be using it to modify the underlying std::string. If you make that const go away with a cast, then sneakily (or more likely accidentally) modify it, then the whole game is over. Your pointer might not even be valid any more (the std::string has deleted itself), or it could be pointing at something else (the std::string has moved).

It's meant for doing things like passing const char * parameter values to historic interfaces which expect a const char *

std::string filename = "foo.txt";
std::ifstream in.open( filename.c_str() );

Keeping one for yourself to do whatever is just inviting later disaster IMO.

A better question is why you're still so desperate to cling to a char* when you're using C++ strings.

c_str() returns a const char * for a reason, that reason being you shouldn't be using it to modify the underlying std::string. If you make that const go away with a cast, then sneakily (or more likely accidentally) modify it, then the whole game is over. Your pointer might not even be valid any more (the std::string has deleted itself), or it could be pointing at something else (the std::string has moved).

It's meant for doing things like passing const char * parameter values to historic interfaces which expect a const char *

std::string filename = "foo.txt";
std::ifstream in.open( filename.c_str() );

Keeping one for yourself to do whatever is just inviting later disaster IMO.

No it works, i tried it.


Why i need to use

char*

is because windows functions only accept char*,

and i need to find substring of the string before i could send it to the windows function.

Ishwar

commented: Can't you follow advice? +0

No it works, i tried it.


Why i need to use

char*

is because windows functions only accept char*

yes they do, and std::string's c_str() returns a const char*. You need to tell us the win32 api function you are attempting to call. But general rule of thumb is that when the parameter is const char* then you can pass std::string's c_str() method. If it is char*, that normally means the function might attempt to change the string, so you have to pass it a char buffer that is modifyable, and not just some character pointer.

> is because windows functions only accept char*,
So how do you know that those functions are not going to modify the string?

Now some windows functions are sloppy in the use of const, but then again perhaps they're not const for a reason. Since you're being vague about what it is you're trying to do, we'll just have to guess.

The only safe way out is something like

char *foo = new char[ str.length() + 1 ];
strcpy( foo, str.c_str() );  // make a copy you know you can change
someAPICall ( foo );  // some function which doesn't like const
delete [] foo;

> No it works, i tried it.
Today, maybe, with your current compiler and particular program.
Long term, in the future, you're gonna get bitten really hard when you least expect it (mostly near a deadline).

Programming is a habit of doing the right thing when it doesn't matter so you'll do the right thing when it does matter. The attitude of "works for me" won't wash long term.

Sometimes, people really do have to fall off a cliff just to figure out gravity exists. Enjoy your future flight.

> is because windows functions only accept char*,
So how do you know that those functions are not going to modify the string?

Now some windows functions are sloppy in the use of const, but then again perhaps they're not const for a reason. Since you're being vague about what it is you're trying to do, we'll just have to guess.

The only safe way out is something like

char *foo = new char[ str.length() + 1 ];
strcpy( foo, str.c_str() );  // make a copy you know you can change
someAPICall ( foo );  // some function which doesn't like const
delete [] foo;

> No it works, i tried it.
Today, maybe, with your current compiler and particular program.
Long term, in the future, you're gonna get bitten really hard when you least expect it (mostly near a deadline).

Programming is a habit of doing the right thing when it doesn't matter so you'll do the right thing when it does matter. The attitude of "works for me" won't wash long term.

Sometimes, people really do have to fall off a cliff just to figure out gravity exists. Enjoy your future flight.

Listen, I don't speak c++, I don't really care what you c++ rabis debate about evry day.

In school we do Pascal , Pointers don't come into play until 1st year university.

Thanks for the advice anyways,


Ishwar

commented: "...you c++ rabis..." Now, is that anyway to talk to some one trying to help you?! -2

Listen, I don't speak c++, I don't really care what you c++ rabis debate about evry day.

In school we do Pascal , Pointers don't come into play until 1st year university.

So what are you trying to tell us? If you expect C++ to behave like Pascal, you better do what you are trying to do in Pascal. Dont use C++.

If you want to learn C++, learn it right. Learn to follow advice. Otherwise you will be programming Pascal the rest of your life even though you will think you are programming in C++.

The fact that you haven't learnt pointers hasn't stopped you from playing with pointers, so either stop using pointers until your university starts pointer lessons, or try to understand why the above advice was given. Saying that you don't care about the advice given will be bad for YOU in the long run.

> Listen, I don't speak c++, I don't really care what you c++ rabis debate about evry day.
Why are you here - really?
It can't be to actually learn anything because you're just being a bonehead about learning anything new. You're just after a quick fix to get past the current problem and be damned to the future.

Great - you've got your free cookie, now go back and enjoy your Pascal for a few more years.

> Thanks for the advice anyways,
But you're just going to ignore it because you've latched onto the first thing which "works for me" and you're happy?

If you don't want advice, don't ask for it.

Listen, I don't speak c++, I don't really care what you c++ rabis debate about evry day.
In school we do Pascal , Pointers don't come into play until 1st year university.
Thanks for the advice anyways,
Ishwar

Frankly one sentence, PLEASE APOLOGIZE THIS INSTANT.
It really hurts when someone goes to the length of wasting his precious time and effort to help a newbie get his path right and all they get is this. Just put urself in our shoes and you would know what i am talking about. I wont adopt the aggressive attitude of Mr. Wolfpack and Mr. Salem and say sarcastic things coz i dont think i have the knowledge set they possess.

Even if you didnt like the anwer or have that PASCAL thing in your head, you could have atleast pretended that the answer helped you than throwing out things like "i really dont care". One thing you should keep in mind that no one in this forum is bonded to this forum in any agreement and formal sort of way. Its just a voluntary service all the people here provide so that newcomers dont have to go through all that these experts had to go through and show a path of correct and precise thinking.

Just think about it and you would know what you may have wanted to said and what you actually ended up saying.

commented: Nicely Said [Grunt] +1

Frankly one sentence, PLEASE APOLOGIZE THIS INSTANT.
It really hurts when someone goes to the length of wasting his precious time and effort to help a newbie get his path right and all they get is this. Just put urself in our shoes and you would know what i am talking about. I wont adopt the aggressive attitude of Mr. Wolfpack and Mr. Salem and say sarcastic things coz i dont think i have the knowledge set they possess.

Even if you didnt like the anwer or have that PASCAL thing in your head, you could have atleast pretended that the answer helped you than throwing out things like "i really dont care". One thing you should keep in mind that no one in this forum is bonded to this forum in any agreement and formal sort of way. Its just a voluntary service all the people here provide so that newcomers dont have to go through all that these experts had to go through and show a path of correct and precise thinking.

Just think about it and you would know what you may have wanted to said and what you actually ended up saying.

Ok I Apologise, I realize I shouldn't have said that.

You guys were more than helpful to me.


Ishwar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.