CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2007
    Posts
    35

    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

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

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

  3. #3
    Join Date
    Sep 2007
    Posts
    35

    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*?

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    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

  5. #5
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    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");
    }
    Last edited by PredicateNormative; June 3rd, 2008 at 07:02 AM.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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;
    Last edited by GCDEF; June 3rd, 2008 at 08:42 AM.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Convert char array to char*

    ^You would of course need to add
    digits2[4] = 0;
    for that to work.

  9. #9
    Join Date
    Sep 2007
    Posts
    35

    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

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured