Can I increase the char * value passed to the function?

I am working on a C ++ application that will build a fixed-length record from a set of database fields. I am writing a function that will take the output record as char*, a string for writing, and the total length of the field. The purpose of this function is to copy the string to the current position of the char pointer, and then fill the rest of the length with spaces. Here's a simplified example of what I'm doing.

void writeOut(char* output, string data, const int length) {
    if ((int) data.size() > length) {
        //Just truncate it
       data = data.substr(0, length);
    }
    int index = 0;
    while (index < (int) data.size()) {
        *output++ = data[index++];
    }
    while (index++ < length) {
        *output++ = ' ';
    }
}

int test() {
    char output[100];
    writeOut(output, "test1", 10);
    writeOut(output, "test2", 10);
    writeOut(output, "test3test4test5", 10);
    cout << output;
}

I expected to see something like this.

test1     test2     test3test4                  

Instead, I get ...

test3test4

Thus, it increases the value char*inside the function, but only inside the function. When the function ends, it char*will return to where it was started. Is it possible to pass a pointer so that the pointer is updated in the calling function?

, ++. .

+3
7

, .

.

void writeOut(char*& output, string data, const int length)

string const string&, .

+8

char ** .

void writeOut(char** output, string data, const int length) {
    if ((int) data.size() > length) {
        //Just truncate it
       data = data.substr(0, length);
    }
    int index = 0;
    while (index < (int) data.size()) {
        *(*output)++ = data[index++];
    }
    while (index++ < length) {
        *(*output)++ = ' ';
    }
}

int test() {
    char output[100];
    char *pos = output;
    writeOut(&pos, "test1", 10);
    writeOut(&pos, "test2", 10);
    writeOut(&pos, "test3test4test5", 10);
    cout << output;
}

( , )

+8

, , , "" , ( cout).

, - :

char* writeOut(char* output, string data, const int length) {
    if ((int) data.size() > length) {
        //Just truncate it
       data = data.substr(0, length);
    }
    int index = 0;
    while (index < (int) data.size()) {
        *output++ = data[index++];
    }
    while (index++ < length) {
        *output++ = ' ';
    }
    return output; // return the new position here!
}

int test() {
    char output[100];
    char *outputLocation = output;
    outputLocation = writeOut(outputLocation, "test1", 10);
    outputLocation = writeOut(outputLocation, "test2", 10);
    outputLocation = writeOut(outputLocation, "test3test4test5", 10);
    cout << output;
}
+6

, , writeOut, ( , ).

, , writeOut. :

- , " ":

void writeOut (char *& output, ...) {
   // ...
}

int test () {
  char output[100];
  char * p = output;
  writeOut (p, "test1", 10);
  writeOut (p, "test2", 10);
  // ...
}

, output p . , writeOut, p.

- :

char * writeOut (char * output, ...) {
   // ...


   return output;
}

int test () {
  char output[100];
  char * p = output;
  p = writeOut (p, "test1", 10);
  p = writeOut (p, "test2", 10);
  // ...
}

p.

, writeOut :

void writeOut (char ** output, ...) {
   // ...

   while (index < (int) data.size()) {
     **output = data[index++];
     (*output)++;
   }

   // ...
}

int test () {
  char output[100];
  char * p = output;
  writeOut (&p, "test1", 10);
  writeOut (&p, "test2", 10);
  // ...
}

, , "", "p". , .

+2

.

0

, , .

, , , . , test() printOut(), printOut() test().

Like others, you can either return the new value of the pointer, or pass it as a reference. Or you can use a pointer to a pointer that will allow you to update the pointer in printOut().

0
source

Why not just use snprintf?

int writeOut(char *output, char *data, int length)
{
  return snprintf(output, length + 1, "%-*s", length, data);
}

void test()
{
  char output[100];
  char *outputLocation = output;
  outputLocation += writeOut(outputLocation, "test1", 10);
  outputLocation += writeOut(outputLocation, "test2", 10);
  outputLocation += writeOut(outputLocation, "test3test4test5", 10);
  printf("%s\n", output);
}

It seems to me easier. To you, I think.

0
source

Source: https://habr.com/ru/post/1718836/


All Articles