Archive | 9:14 pm

## Rodeo horse sketch, part 3

17 Aug

Well, the carpenter’s pencil is proving to be a very powerful drawing tool to put fast sketches onto paper. This sketch took less than 10 minutes to make. The digital manipulation and writing this blog post took much longer. The important thing to remember while doing such sketches is starting with an overall action line, force line or through line, whatever you want to call it. This line gives the overall idea of the sketch, and what you as an artist want to express. This horse gave me a spring-loaded look and feel, so I tried to recreate that in the drawing.

That is all.

## Passing an array to a function in C

17 Aug

Yes, I admit it. I too have fallen for the iPhone, iPhone programming to be more specific. At this moment, aside from improving my drawing skills, I’m also brushing up my C programming skills, using the (out of date) book “Beginning Mac OS X Programming”. If you’re not into programming, just skip this blog post.

Here’s the problem. In the C language, how do you pass an array to a function, manipulate it and return it as an array value? The problem seems to be that if you use `sizeof` to determine the length of the array, that length isn’t known at compile time. The content of the array may change while the program runs. So the best practice seems to be to determine the length of the array before you call the array function, and pass the length along as well.

Here is the code to demonstrate my point.

#include

// Prototypes
void printArray(int ar[], int n);
int* revArray(int ar[], int n);

int main (int argc, const char * argv[]) {
int ar[] = {1, 2, 3, 4, 5, 6, 7};
// sizeof gives size in bytes
int n = sizeof(ar) / sizeof(int);
printArray(ar, n);
printArray(revArray(ar, n), n);

return 0;
}

// Print all array elements.
void printArray(int ar[], int n) {
for (int i=0; i < n; ++i ) printf("%d ",ar[i]); printf("\n"); } // Reverse array elements. int* revArray(int ar[], int n) { int v, j; for (int i=0; i < n/2; ++i) { j = n - i - 1; v = ar[i]; ar[i] = ar[j]; ar[j] = v; } return ar; }[/sourcecode] I tried this code without passing along the length of array `ar`. I tried to determine the array length within the called function instead of in the part of the code where the array was defined (in this case, `main`). That didn’t seem to work. After Googling in vein, I looked it up on Stack Overflow (which is, by the way, the best resource for questions about programming on the Net). And there was the answer, as I just told you. Well, actually it was a question about C++, but who’s counting? Still the explanation in the answers were a bit puzzling, so I tried some reasoning.

Here’s what I think is happening.

In C you pass parameters to a function by value only. In case of an array, the value of a pointer to the array is passed. This value is used to initialize the pointer that was defined in the function parameter list. Apparently, C has no means of passing the length of that array as well.

In fact, you only pass the pointer to the first array element to the function. The function cannot know how many elements follow after the first element, so the function caller has to tell the function, by passing the value of the length of the array.

Of course, in `main` the array `ar` is in context, so `sizeof(ar)` will give you the number of bytes that is reserved for `ar`. If you divide that by the number of bytes that is reserved for an `int` value (which you write in C as `sizeof(int)`), you have the number of array elements in an integer array.

That is all.