简体   繁体   English

字符点内存分配到 C++ 字符串的引用地址?

[英]Char Point Memory Allocation to reference address of C++ String?

I would like to convert an array of Integers 2, 3, 4, 8 5, 7, 9, 12 1, 0, 6, 10我想转换一个整数数组 2, 3, 4, 8 5, 7, 9, 12 1, 0, 6, 10

to a string with the entries of that matrix appended in clockwise order “2, 3, 4, 8, 12, 10, 6, 0, 1, 5, 7, 9”.到一个字符串,该矩阵的条目按顺时针顺序附加“2, 3, 4, 8, 12, 10, 6, 0, 1, 5, 7, 9”。

I have to keep declaration of int * Matrix and char * OutBuffer the way they are我必须按char * OutBuffer保留int * Matrixchar * OutBuffer的声明

int main()
{
    int matrixArray[rowCount][columnCount] =
    {   {2, 3, 4, 8},
        {5, 7, 9, 12},
        {1, 0, 6, 10}};

    int * matrix;
    string prebuffer;
    char  * outBuffer;

    outBuffer = new (nothrow) char[24];
    matrix = &matrixArray[0][0];

    BuildStringFromMatrix(matrix, rowCount, columnCount, outBuffer);
}

I declare and address all my pointers before passing them in. However, I am not sure if I am going about allocating memory for the outBuffer to store the characters of prebuffer correctly?我在将所有指针传入之前声明并寻址它们。但是,我不确定是否要为outBuffer分配内存以正确存储outBuffer的字符?

void BuildStringFromMatrix(int* Matrix, int NumRows, int NumColumns, char * OutBuffer)
    {
        string prebuffer;
        bool stringLeft = true;
        int i = 0;

        while (stringLeft)
        {
            int clockwiseDir[12] = { 1,1,1,4,1,1,0,4,-4,-1,-1,-1 };

            prebuffer = to_string(Matrix[i]) + ", ";
            OutBuffer = new char [prebuffer.length() + 1];
            cout << prebuffer;
            i += clockwiseDir[i];

            if (i == 6)
            {
                prebuffer = to_string(Matrix[i]) + " ";
                cout << prebuffer;
                stringLeft = false;
            }
        }
    }

**When I do not implement OutBuffer I have no trouble accessing and printing the matrix in clockwise format **当我不实现OutBuffer我可以按顺时针格式访问和打印矩阵

But I how would I go about using OutBuffer to reference and print prebuffers contents??但是我将如何使用 OutBuffer 来引用和打印预缓冲内容? I need numbers to display not unprintable symbols on the ASCII table我需要数字来在 ASCII 表上显示不可打印的符号

Thanks in advance :) **提前致谢 :) **

Firstly, in your loop under BuildStringFromMatrix function you are not using your i value anywhere.首先,在BuildStringFromMatrix函数下的循环中,您没有在任何地方使用 i 值。 Second, matrix = matrixArray should do fine.其次, matrix = matrixArray 应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM