Home > front end >  Is there another way to return an array in C instead using pointers?
Is there another way to return an array in C instead using pointers?

Time:05-27

I thought I could return an array like I do in Java, but I think there is no way to declare a char function in C.

CodePudding user response:

No. You return an array via a pointer (possible embedded in a struct). The array is either dynamically allocated in the function, or passed into the function as an argument and then returned again:

char *f() {
  char *s = malloc(42);
  ...
  return s;
}
char *f2(char *s) {
  ...
  return s;
}
  • Related