0
void update(char array[10]) {
  // function implementation
}
int main(int argc, char **argv) {
  char array[10] = "welcome";
  update(array);
  return 0;
}

I am trying to pass a pointer-to-array to another function without losing its array metadata. In other words, when I am debugging and control jumps inside "update" function, I no longer can inspect the state of each of my 10 characters as they get modified by the "update" function because control is no longer within the scope where the array was created. Once control jumps out of the "update" function and returns to the "main" or calling function, only then I can see the updated version of the referenced array.

Is there a way to fix this?

As a side note, I am using the built-in debugger that comes with Visual Studio Code as well as the GCC compiler.

  • @Oka when the array of 10 characters is created within the scope of "main" function, I can expand all 10 characters using the debugger tool. However, once control jumps inside "update" function, I can only see the first character of the array. In other words, the array metadata is lost. –  Nov 08 '22 at 20:43
  • The metadata that you lose is the length of the array, since you transition from having a fixed length array to having a pointer to the beginning of a fixed, but unknown, length array. The way you do it might be different, but it should still be possible to view the contents. If you specify the debugger you are using, perhaps someone familiar with that specific debugger can help. And thumbs up for using a debugger! – Avi Berger Nov 08 '22 at 20:46
  • @AviBerger I am using the built-in debugger that comes with Visual Studio Code. In addition, I am using the gcc compiler. –  Nov 08 '22 at 20:48
  • I've never used Visual Studio Code, but edit your post to add that information. – Avi Berger Nov 08 '22 at 20:51
  • 1
    [Try this perhaps.](https://stackoverflow.com/q/52721440/631266) – Avi Berger Nov 08 '22 at 20:58
  • You *are not* passing a pointer to an array. You are passing a pointer to an array **element** (in particular, the first one). You can use that pointer to access *all* the elements, but as it itself is a pointer to just one of them, it does not convey the size of the array. You have to either assume that or receive it as a separate argument. – John Bollinger Nov 08 '22 at 21:19

0 Answers0