but for char ? not for string ...

So i want something like this:

char subs[];
char s[] = "Hello baby";
strmid(subs,s,0,4);
puts(subs);

will print Hello ...

Could try:

char array1 [25];  //With the letter in it already...
string string1(array1);

and the use substr...

Are you trying to return a char?

Your in the C++ section, why can't you use string?

LOL, that's what I was wondering.... But he may have a reason to return char.... Waiting for an answer back.

i want to return a char because i tryed to make a function called strmid ...

std::string is just a wrapper around a char * and from reading the function you do not want to change it back to char[] anyway

If you must you would still go via std::string str.c_str() converts a string back to the array pointer which is what you are doing when you don't set a size you set a char array1[] = "hello"; you have a pointer called array1

#include <string> // std::string
#include <iostream> //output
int main()
{
 //this is the same as char array1[] 
 char array1[] = "hello baby";
 std::string temp = array1;
 int first(0), last(5);
 char small_temp  = temp.substr(first, last - first);
 //write the string
 std::cout << "small_temp = " << small_temp << std::endl;
 char  array2[] = small_temp.c_str();
 return 0;
}

basically although you appear not to realise it you want to use a
std::string and if you need a char * or char [] just use a std::string with .c_str() function.

Edited 6 Years Ago by tetron: minor typo

This article has been dead for over six months. Start a new discussion instead.