Click to See Complete Forum and Search --> : Convert char array to char*


motorollin
June 3rd, 2008, 02:24 AM
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

JohnW@Wessex
June 3rd, 2008, 02:50 AM
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'.

motorollin
June 3rd, 2008, 03:01 AM
Ok, I can make it a string:


string alldigits("");
alldigits += digits[1];
alldigits += digits[2];
alldigits += digits[3];
alldigits += digits[4];


But how do I then make that a char*?

Paul McKenzie
June 3rd, 2008, 03:53 AM
Ok, I can make it a string:


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

PredicateNormative
June 3rd, 2008, 07:00 AM
Something along the lines of:


#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");
}

Lindley
June 3rd, 2008, 07:01 AM
If you don't mind modifying the original data, the simplest way is:


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....

GCDEF
June 3rd, 2008, 07:29 AM
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;

Lindley
June 3rd, 2008, 07:54 AM
^You would of course need to add
digits2[4] = 0;
for that to work.

motorollin
June 3rd, 2008, 08:37 AM
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

GCDEF
June 3rd, 2008, 08:42 AM
^You would of course need to add
digits2[4] = 0;
for that to work.

Corrected. Thanks.