todayitryto: explain the point of pointers in C

“Passing by reference”, “how to pass an array to another function”
I try to explain the point of pointers in CJust in case you didn’t know what they are
A pointer is like any other variable, except they hold the location of another variable. The location that I’m talking about is a variable’s address in memory.
Back when C was made, the ability to work with memory locations was vital since computers back then weren’t as powerful and being very very efficient is a necessity.

Pointers in C also gave it features like arrays, strings (it’s just an array of characters), and being able to dynamically store data, and being able to efficiently pass data between functions.

Not completely related but if you are looking for an article like this one then these will probably help you too
Difference between pass by reference and pass by value
Passing by reference means the caller and the callee are using the same variable.
Passing by value means the caller and the callee are using a copy of the same variable but are each working on their copy of that variable.

How to pass an array to another funciton in C
lets pretend we have a function named arraytest that accepts an array of int and that we have an array named arr

void arraytest(int a[]);

int arr[2] = {0,1};

To pass arr to arraytest we just simply


Since passing an array name is the same as passing a pointer to the first element of that array (from which you can access the rest of the array), this is considered passing by reference.

How about returning an array?
C won’t let you return arrays but you can return a pointer to an array that has been dynamically allocated (or at least that’s what I do, (malloc incoming))

int* j = malloc(sizeof(int)*ARRAY_SIZE);
return j;

Hope this helped someone


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s