Originally Posted by
ObjectWithBrain
So if the array is local to printBin(), like in the last version I showed which didn't work, is there any way to pass the value of binString (or buffer) without passing the pointer/array itself? (since obviously I can't do that).
Here's a little code example you can run to demonstrate why you should never try to return arrays from functions...
Code:
#include <stdio.h>
int* MyFunction(int a, int b, int c)
{ static int array[3];
array[0] = a;
array[1] = b;
array[2] = c;
return array; } // return a pointer.
int main (void)
{ int *a1, *a2; // int pointers
printf("calling a1 = MyFunction(10,20,30);\t");
a1 = MyFunction(10,20,30);
printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
printf("calling a2 = MyFunction(100,200,300);\t");
a2 = MyFunction(100,200,300);
printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
printf("\nLooks good, except...\t");
printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
getchar();
return 0; }
If you remove the static keyword the array is destoryed as the function returns and things get even stranger.
The way to deal with this is to pass a pointer to the array into the top of the function and operate on the remote array.