Combine char to get char*

时间:2016-04-04 17:06:44

标签: c++ c

Really dumb c++ question, but I don't see it covered in the way I need it to be. I have a const char* that I want to pull various values out of and into their own char*. For example

const char* atr;
//
//Data is placed in atr
//

//Here's what I want to do, but obviously this doesn't compile
char* _ts = atr[0] + atr[1];
char* _t0 = atr[2] + atr[3];

Thanks a ton in advance. I'm a day to day C++ dev who was basically raised on C++11 and beyond, so dealing with character pointers is a bit strange to me.

5 个答案:

答案 0 :(得分:2)

Unlike C++, C does not let you overload operators, which is the feature responsible for std::string concatenation using operator +. Therefore, in C you get the sum of two character codes, and you write them into a character pointer. That is not what you want.

If the number of locations from which you are picking characters is fixed, you can construct C strings using initialization:

char _ts[] = { atr[0], atr[1], 0 };
char _t0[] = { atr[2], atr[3], 0 };

Note null terminators at the end, this is required when you wish to use _ts and _t0 as C strings.

Demo.

答案 1 :(得分:1)

If you really want to use chars instead of string, here's your problem: You didn't initialize your pointers. Try with char* my_ptr = new char(atr[0]);

答案 2 :(得分:0)

This function will make a string consisting of the specified two characters. Don't forget to free it when you're done with it.

char* makeString (char c1, char c2)
{
    char* buf = malloc(3);
    buf[0] = c1;
    buf[1] = c2;
    buf[2] = 0;
    return buf;
}

So now you can do this:

char* _ts = makeString (atr[0], atr[1]);

答案 3 :(得分:0)

_ts and _t0 are pointers to memory locations of type char. on the right hand side of equal sign where you do atr[0] + atr[1] what is happening there is you are first de-referencing the memory locations of atr and (atr+1) and getting the values there. note: atr+1 is 1 byte away from atr, because atr is of type char and the sizeof(char) is 1 byte. so now you have two character values from atr[0] and atr[1] and even though they are of type char, those values can be added because in the C language the character type is really a 1 byte integer number between 0 and 255 based on the ascii table. for example, the value of 'A' is decimal 65. The value of 'a' is decimal 97. Which is why something like this works:

#include <stdio.h>
#include <stdlib.h>

int main ( int argc, char *argv[] )
{
    char c1, c2, c3;

    c1 = 'A';   /* ascii value of 65 */
    c2 = 'd';   /* ascii value of 100 */

    printf("decimal value of c1 and c2 is %d and %d\n", c1, c2 );

    if (( c1 >= 65 ) && ( c1 <= 90 ))
    {
        printf("I know c1 contains a letter between capital A and capital Z,\n");
        printf("it's decimal value is %d which is the character %c\n", c1, c1 );
    }


    c1 = 'A';   /* ascii 65 */
    c2 = 'a';   /* ascii 97 */
    c3 = c2 - c1;
    printf("c1 = %c, c2 = %c\n", c1, c2 );
    printf(" c2 - c1 = %d, x%cx\n", c3, c3 );

    /* ascii of 97-65 = 32 == space character */

    c1 = 67;  /* set c1 to a capital C */
    printf(" c1 = decimal %d, it is character %c\n", c1, c1 );

    return 0;
}

http://www.asciitable.com/

答案 4 :(得分:0)

With C99, code can use compound literals. Code creates two 3-char arrays and populates them with elements of atr[].

const char* atr = "abcd";

char* _ts = (char [3]) {atr[0], atr[1], '\0'};
char* _t0 = (char [3]) {atr[2], atr[3], '\0'};

puts(_ts);
puts(_t0);

Output

ab
cd

The pointers _ts, _t0 are valid until the end of the block.