Passing Arrays by Reference in C
From Scriptionary.com
The question How do I pass an Array by Reference in C is a common one in C programming. This small article will attempt to explain how to accomplish this and what exactly happens. This article does not require additional APIs, only the standard C Input and Output library, STDIO if the user wishes to add printed statements (not included by default).
Article Sample Code
The code use throughout this article is listed below. When a reference is made to a variable name, please look it up in the following code:
#include <stdio.h> #define FAIL 1 #define SUCCESS 0 // function not defined yet int FillOutArray(/* arguments placeholder */) { return FAIL; // not yet implemented }; int main( int argc, const char* argv[] ) { unsigned int myArray[10] = {0}; // not yet defined return SUCCESS; // not yet implemented }; // main
Please note that the actual code will be implemented in the sections below.
Passing by Reference
Normally when we pass anything by reference in C, the function might look like so:
void iPassBy(int* myInt) { myInt = myInt * 5; }; // iPassBy
To pass an array to a function, the exact same syntax is used:
#include <stdio.h> #define FAIL 1 #define SUCCESS 0 int FillOutArray(unsigned char* theArray) { return FAIL; // not yet implemented }; int main( int argc, const char* argv[] ) { unsigned char myArray[10] = {0}; // not yet defined return SUCCESS; // not yet implemented }; // main
Which shows that we don't include the brackets [ ] to denote that we're using an array; we're simply passing an unsigned character pointer. Accessing elements in the array is also done fairly simply:
#include <stdio.h> #define FAIL 1 #define SUCCESS 0 int FillOutArray(unsigned char* theArray) { unsigned int size = sizeof(theArray); for (int i = 0; i < size; i++) { theArray[i] = (i < 256) ? i : 255; // fill out incrementally. } return SUCCESS; }; int main( int argc, const char* argv[] ) { unsigned char myArray[10] = {0}; return FillOutArray(myArray); }; // main
Passing a Referenced array as a Reference
Something more complicated would be passing the referenced array theArray as a reference to a function such as fread or any other that requires a referenced parameter. In the following example, the array will be filled out with values stored in a binary file.
#include <stdio.h> #define FAIL 1 #define SUCCESS 0 int FillOutArray(unsigned char* theArray) { const char *filename = "MYFILE.BIN"; unsigned int size = sizeof(theArray); FILE *fileHandle; fileHandle = fopen(filename, "rb"); if (fileHandle == NULL) return FAIL; if ( fread(*&theArray, 1, size, fileHandle) != size ) return FAIL; return (fclose(fileHandle) == 0) ? SUCCESS : FAIL; }; int main( int argc, const char* argv[] ) { unsigned char myArray[10] = {0}; return FillOutArray(myArray); }; // main
The important thing to notice here is *&theArray which is a pointer to the address of theArray, which fread can accept.
BlogMarks
del.icio.us
digg
Fark
Furl
Newsvine
reddit
Segnalo
Simpy
Slashdot
smarking
Spurl
Wists
