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.

10
  • 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).) Commented Jun 10, 2013 at 21:01
  • 1
    Read section 6 of the comp.lang.c FAQ. Commented Jun 10, 2013 at 21:05
  • @KeithThompson - Sorry for the distraction. A typo Commented Jun 10, 2013 at 21:48
  • 1
    void main(void) is no better than void main(). It's int main(void). Commented Jun 10, 2013 at 21:49
  • 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. Commented Jun 10, 2013 at 22:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.