c array as parameter

I have looked online, and this still doesn't seem to work. I have tried rewriting it in multiple ways but it always either prints garbage instead of the string, reports an error, or doesn't modify the address like it should at the end of the function.

This is my code, and what it should do:

String parseNextSegment(char *buffer, int bufferLen){
  String parsed = "";
  int i = 0;
  //copy chars into string until semi-colon is found
  while (buffer[i] != ';'){
    parsed += buffer[i];
    i++;
  }
  i++;
  
  char *newBuf = new char[bufferLen - i - 1];
  //copy the rest of the data into a new buffer
  for (int ni = 0 ;i < sizeof(buffer); ni++, i++){
    newBuf[ni] = buffer[i];
  }
  
  buffer = newBuf;
  return parsed;
}

void loop(){
  char *chars = {"hello;hohoho;hallo\0"};
  Serial.println(parseNextSegment(&chars,19));
  Serial.println(parseNextSegment(&chars,19));
  Serial.println(parseNextSegment(&chars,19));
}

I wrote it how I think it should be written, but this doesn't compile, saying: "sketch_sep21a:26: error: cannot convert 'char**' to 'char*' for argument '1' to 'String parseNextSegment(char*, int)'"

What this function is supposed to do is extract the first segment of a string, seperated by a semi-colon, and return that. The remaining elements of the array are loaded into a new buffer, then the address of the new buffer is supposed to be assigned to the original buffer.

Any help is appreciated :-);

Serial.println(parseNextSegment(&chars,19));

Lose the &

When the & operators are removed, the program doesn't execute. No errors, but nothing happens.

When the & operators are removed, the program doesn't execute

I won't go so far as to say that's impossible, but it's very, very unlikely.

It doesn't seem to execute. There is no output, I even changed it to include a print as the very first statement.

void setup(){}

String parseNextSegment(char *buffer, int bufferLen){
  String parsed = "";
  int i = 0;
  //copy chars into string until semi-colon is found
  while (buffer[i] != ';'){
    parsed += buffer[i];
    i++;
  }
  i++;
  
  char *newBuf = new char[bufferLen - i - 1];
  //copy the rest of the data into a new buffer
  for (int ni = 0 ;i < sizeof(buffer); ni++, i++){
    newBuf[ni] = buffer[i];
  }
  
  buffer = newBuf;
  return parsed;
}

void loop(){
  Serial.println("begin");
  char *chars = {"hello;hohoho;hallo\0"};
  Serial.println(parseNextSegment(chars,19));
  Serial.println(parseNextSegment(chars,19));
  Serial.println(parseNextSegment(chars,19));
}

What speed is the serial port set to?

Ha, that was it, I didn't call Serial.begin. Now the only problem is that when this function executes, it does not replace the pointer to the original buffer with the new one. I tried directly changing the data on the original buffer, but that had no effect, as if the array was somehow passed by value. From what I understand, arrays can only be passed by reference or as pointer right?

  buffer = newBuf;

What do you think this is doing? It is NOT copying the data from newBuf to buffer. Changing the value of the argument is pointless, since the value is not returned to the function.

The reason for passing a pointer to the function is so that you can change the data where the pointer points, not so that you can change where the pointer points.

Edit: Are you freeing the memory that new allocates somewhere? Or is running put of memory OK?

That line was supposed to change the pointer from one address to another. I had tried directly modifying the original array, by changing:

    newBuf[ni] = buffer[i];

To:

    buffer[ni] = buffer[i];

This did not have an effect though, and the data was not changed.

This did not have an effect though, and the data was not changed.

I'm sure that they can explain why at http://snippets-r-us.com