failing like never before

25Mar/080

Passing an Array in C

This quick little article is intended to give you a better idea of how arrays are passed in C. A basic understanding of pointers is required. If you don't know what pointers are, check them out at wikipedia.

We'll be using the following array as an example throughout the article:

char c_array[50];

The variable that we use to reference the array is actually a pointer. A pointer points to a location in the computer's memory, as you probably already know. You can have pointers to any data type you want, even a pointer to a pointer. The reason why we can reference all 50 elements of our array with just one pointer, is because arrays are allocated as one big fat chunk of memory. That is, all the elements in the array are lined up in the computer's memory one after the other. This is very handy, because if we know the location of the zero element in the array (the pointer to an array points to the zero element) we can get the physical location in memory of the fifth element (I made a funny!) by doing something like:

c_array + 4

Of course, thats a rather impractical way of accessing data, so we usually just end up doing:

c_array[4]

(Notice that I used a 4 to access the fifth element, because in Computer Science we always begin counting from 0, and not 1)

The fact that arrays are referenced by pointers, can be both good and bad. If your array is quite large, then passing by value can be quite expensive (that is, if you could pass an array by value, arrays in C have to be passed by reference). However, as you probably know, passing by reference can be a problem because you may want to leave your old array untouched. When passing an array, if you want to make sure your passed array isn't changed or modified you can either pass it as const, or else create a new array and copy all your old data into it. You can use strcpy from the standard library to copy the array into a new one.

3Feb/084

Draw Me In

I looked through my stats and log files today (instead of doing my homework), and determined which articles were drawing the most visitors to my site. Mind you, this was not an in-depth study, just a cursory glance at my stats.

  1. Dynamically Create a Multidimensional Array in C/C++ - The most popular article on my site was a new one. It would appear that lots of people were searching for ways to dynamically create a multidimensional array and Google kindly linked them to my site.
  2. more stocksquest - This one surprised the crap out of me (pardon my Klatchian). Back in high school, I had nothing else to put on my blog, so I posted my econ class's stocksquest rankings (notice who's number one). It would appear that lots of people use internet search engines to search for their own name. Since pretty much none of my classmates were vaguely famous on the internet, the ones with fairly unique names ended up at my site. Funny how that works out. I bet none of them ever realized whose site they found themselves looking at.
  3. problems with gallery - So apparently lots of people have problems with an uninitialized constant when using attachment_fu. Back when I was working on my blog CMS, I had a problem with attachment_fu and I posted the error message on my blog. I found a solution eventually, which I still remember today because the error was particularly annoying. Basically what happened was there was a misspelled variable name, and if I remember correctly, I copied that code fragment from another site (which would explain why everyone is having the exact same error). I remember that I spent forever looking and it turned out that one of the variable names was spelled beginning with a lowercase letter, which resulted in the uninitialized error. Sorry that I can't remember the exact name of that variable or where it was, but it was almost a year ago.

And thats it folks...

20Jan/080

Dynamically Create a Multidimensional Array

I had a few problems trying to dynamically create and pass a multidimensional array in C++ for my previous programming project. Of course, passing an array by value is rather ridiculous when the array is large, so I had to pass by reference. Now, passing a single dimensional array is really quite simple in C++, you just do the following:

//stuff
int input_cols;
char *char_array_ptr;
//stuff
cout << "cols? ";
cin >> input_a;
char_array_ptr = new char[input_cols];
return char_array_ptr;

And hey, you're done. The same thing could pretty much be done in C by using malloc, instead of new. But as I mentioned, I needed to pass a multidimensional array.