Convert char array to char*
Probably another stupid question :) I need to convert an array of char to a single char*. But not all elements - I want to ignore the first and last element. So, if char digits[6] contains the following:
digits[0] = "A";
digits[1] = "B";
digits[2] = "C";
digits[3] = "D";
digits[4] = "E";
digits[5] = "F";
then I want char * alldigits to contain "BCDE". I have spent ages looking for a solution to this, but can't find anything that works!
TIA
Re: Convert char array to char*
You'll have to copy the characters into a new string, as the last element in a 'C' string must be '\0'. Doing this to your original array would require you to overwrite the 'F' with '\0'.
Re: Convert char array to char*
Ok, I can make it a string:
Code:
string alldigits("");
alldigits += digits[1];
alldigits += digits[2];
alldigits += digits[3];
alldigits += digits[4];
But how do I then make that a char*?
Re: Convert char array to char*
Quote:
Originally Posted by motorollin
Ok, I can make it a string:
Code:
string alldigits("");
alldigits += digits[1];
alldigits += digits[2];
alldigits += digits[3];
alldigits += digits[4];
But how do I then make that a char*?
First, the terminology is incorrect.
A char* is a pointer. A std::string is a class that represents a character string. They are not the same thing. A char* may point to the start of a character buffer (or just a single char), depending on how it's used.
Second, The std::string.c_str() returns a const char * that represents the data of the std::string.
Third, if you plan on changing what the char* points to, you cannot do it with std::string::c_str(), as the pointer returned is const. Either use a regular char array, use std::vector<char>, or use new[]/delete[] for routines that explicitly want a char * (not const char *).
Regards,
Paul McKenzie
Re: Convert char array to char*
Something along the lines of:
Code:
#include <iostream>
#include <vector>
size_t getSize(std::string str){return str.size();}
int main()
{
char* digits = "ABCDEF";
size_t size = getSize(digits);
std::vector<char> vec;
if (size > 2)
vec.assign(digits+1, digits+size-1);
vec.push_back('\0');
char* result = &vec[0];
std::cout << result << std::endl;
system("PAUSE");
}
Re: Convert char array to char*
If you don't mind modifying the original data, the simplest way is:
Code:
char digits[6];
char *ptr;
digits[0] = "A";
digits[1] = "B";
digits[2] = "C";
digits[3] = "D";
digits[4] = "E";
digits[5] = "F";
ptr = &digits[1];
digits[5] = 0;
Also note that while a char array and a char* are convertible in 1D, the same is *not* true of 2D arrays----you can't convert a char[10][10] into a char**, for instance. This is because of the way the sizeof() operator handles each one....
Re: Convert char array to char*
Quote:
Originally Posted by motorollin
Probably another stupid question :) I need to convert an array of char to a single char*. But not all elements - I want to ignore the first and last element. So, if char digits[6] contains the following:
digits[0] = "A";
digits[1] = "B";
digits[2] = "C";
digits[3] = "D";
digits[4] = "E";
digits[5] = "F";
then I want char * alldigits to contain "BCDE". I have spent ages looking for a solution to this, but can't find anything that works!
TIA
chars are represented by single quotes. The code you posted shouldn't compile.
One way to get what you want would be
char digits2[5];
strncpy(digits2, digits + 1, 4);
digits2[4] = 0;
Re: Convert char array to char*
^You would of course need to add
digits2[4] = 0;
for that to work.
Re: Convert char array to char*
Thanks for the help guys. I really don't know what to do with this, and to be honest that stems from not understanding the various data types in C++. I'll go and do some reading about that and then come and have another look. Hopefully it will make more sense then!
Cheers
Re: Convert char array to char*
Quote:
Originally Posted by Lindley
^You would of course need to add
digits2[4] = 0;
for that to work.
Corrected. Thanks.