2

I have just started my back-to-the-roots project; learning C. I'll be honest, abstraction is nice when you need to plow out a desktop/server application, but with C, things get personal. Which is nice, for a change!

Now to the point; I'm reading into using arrays with functions (Programming in C, Stephen G. Kochan.) I have learnt that when passing an function as a parameter the compiler will always see the reference as a pointer, like so:

void foo (char *array);

Take this, for example:

void foo (char *a) 
{
    a[0] = 'R'; // side effect
}

int main (void) 
{
    char a[] = "The quick brown fox jumps over the lazy dog!";
    foo (a);
}

I haven't come by information on functions returning arrays or pointer to an array. The function rather changes the array in its parameter, thus causing side effects (as seen above).

Is it possible for a function to return a pointer to an array? Or is the above method just preferred?

Thanks in advance for those that can offer an explanation.

khellang
  • 17,550
  • 6
  • 64
  • 84
mfaerevaag
  • 730
  • 5
  • 29
  • 2
    `void main()` should be `int main(void)`. Please fix your code, and burn whatever book told you to write `void main()`; its author doesn't know the language. (I just checked "Programming in C" by Stephen G. Kochan on Amazon; at least as of the 3rd edition, the examples I was able to see correctly use `int main(void)`.) – Keith Thompson Jun 10 '13 at 21:01
  • 1
    Read section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Jun 10 '13 at 21:05
  • @KeithThompson - Sorry for the distraction. A typo – mfaerevaag Jun 10 '13 at 21:48
  • 1
    `void main(void)` is no better than `void main()`. It's `int main(void)`. – Keith Thompson Jun 10 '13 at 21:49
  • Functions cannot return arrays, period, so the title is a non-starter. – Ed S. Jun 10 '13 at 21:49
  • @JonathanLeffler - The topic is the same, though my question is different. I'm interested in why, not so much how. – mfaerevaag Jun 10 '13 at 21:50
  • @EdS. - As the topic is on a broader scope, I would disagree on that it is a non-starter. And it seems it is very much possible, but as parkydr pointed out, not wise - which was precisely the point of my question. You might be forgetting the word pointer, anyways not very a helpful, nor informative comment. – mfaerevaag Jun 10 '13 at 22:00
  • @KeithThompson - Thanks. Apologize for my sloppiness – mfaerevaag Jun 10 '13 at 22:02
  • 1
    @7SLEVIN: No, it is *not* possible to return an array from a function. n It has nothing to do with being unwise; if you try it you are returning a pointer to the first element, *not* an array. They are different things. One catch; you can wrap the array in a struct. – Ed S. Jun 10 '13 at 22:46
  • @EdS. - Well again, that was the question. Returning an actual array isn't allowed anywhere. Returner a pointer is, but not in C, thus the reason for my curiosity. Seems the logic behind it is what many call this one of the main features of the language. I would love to hear more, but I guess this isn't the place. – mfaerevaag Jun 11 '13 at 05:50

2 Answers2

1

A function can return a pointer to the first element of an array, but it's more problematic, you either have to malloc the memory (like strdup) for the array or have a static array (like ctime).

malloc gives the problem of having to remember to free the memory (memory leaks), static means the array changes with each call, therefore passing an existing array is easier.

parkydr
  • 7,596
  • 3
  • 32
  • 42
  • 2
    It's more common for a function to return a pointer to *the first element of* an array. (You then have to use some other mechanism to determine how long the array is.) – Keith Thompson Jun 10 '13 at 21:06
0

I am not sure I follow. Your function is of type void. Are you trying to modify the array by passing it as reference?

In C, if you return a pointer to the first element of the array, you effective return the entire array because C arrays are contiguous in the programs memory (may be not in physical memory). So when you return the pointer to the first element, you effectively tell your main function where the entire array is stored.

maverick1989
  • 342
  • 7
  • 20