# Passing arrays

• 11-19-2009
renvaar
Passing arrays
Hey all, I'm at the part of my course where I am being taught arrays, and passing them to functions. I have been asked to produce a program that has 2 arrays that the user enters 10 numbers in and then gives the total for each array.

So far i've come up with this :
Code:

```#include <stdlib.h> #include <stdio.h> #define NUMBER_ELEMENTS 10 void f_enter (); void f_display (); void main () { float number[NUMBER_ELEMENTS]; f_enter (number, NUMBER_ELEMENTS); f_display (number, NUMBER_ELEMENTS) ; exit(0); } void f_enter (int input[], int size) {         int i = 0 ;         for (i = 0; i < 10; i ++)         {                 printf("Enter number %d for first array: ", i + 1);                 scanf("%d", &input[i]);                 fflush(stdin);         } } void f_display (int display[], int number_elements) {         int i = 0, total = 0 ;         for (i = 0; i < 10; i ++)         {                 total = total + display[i];         }         printf("total is %d ", total); }```
This does it for one array, but I really just copied some other code from a book that did something similar and it somehow works. I really don't see how the array is being passed and used in the display function, or how I would do this for a second array.

Is there anyone who can help explain whats going on in the code and how its being passed between f_enter and f_display.
• 11-19-2009
quzah
Code:

```void f_enter (int input[], int size) {         int i = 0 ;         for (i = 0; i < 10; i ++)```
What's the point of passing size if you don't use it? The same goes for your other function. Also: 'fflush( stdin )' is undefined behavior, and 'void main' is incorrect. Also, your function prototypes should match your function definitions.

Quzah.
• 11-19-2009
renvaar
Well done, you really helped me with the problem there. And since I am a total beginner and already explained I simply copied sections of code to try and make a working program (which it does work) I know there are some parts that aren't needed or right... but i got it working, when I have no clue how passing arrays work.

and what do you mean about fflush, I was mearly clearing the buffer.
• 11-19-2009
zacs7
Quote:

Originally Posted by renvaar
what do you mean about fflush, I was mearly clearing the buffer.

Cprogramming.com FAQ > Why fflush(stdin) is wrong
• 11-19-2009
renvaar
Well thats what my programming teacher tells me to do, and so I'd rather do what she says and pass the course than refuse to do it and fail.

And great guys, so far 2 posts nit picking at code written by a beginner, and no help what at all about what I was asking.
• 11-19-2009
quzah
Quote:

Originally Posted by renvaar
Well done, you really helped me with the problem there.

I quoted the section of code that specifically showed you what was wrong. Here, since you're being a grump, let's add some color:
Code:

```void f_enter (int input[], int size) {         int i = 0 ;         for (i = 0; i < 10; i ++)```
Why are you using 10, when you should be using size? That's my point. You need to look at each function as its own little world. It only knows about what's between the opening { and the closing }. The exception is global variables, and functions which it knows about ahead of time. But, you need to look at your function and think "why does this function know what 10 is"? ... It doesn't. It shouldn't rather. You should instead be using size, since that's what your function is supposed to be knowing about:
Code:

`f_enter( somearray, thatarraysize );`
See? Your function shouldn't know, without using 'thatarraysize', what the real size of the array is. You shouldn't hard code that in, because what happens if 'thatarraysize' is bigger or smaller than 10? Your program won't work right.

PS: Don't be such a whiner.

Quzah.
• 11-19-2009
renvaar
I wasn't whining, but you are telling me about something I know is wrong, and changed shortly after posting the code. And ignoring what I actually need help with, which is understanding whats going on. As a beginner I want to understand the program, not the tiny little mistakes I make as a beginner (but leave the program still working)
• 11-19-2009
Quote:

Originally Posted by renvaar
Hey all, I'm at the part of my course where I am being taught arrays, and passing them to functions. I have been asked to produce a program that has 2 arrays that the user enters 10 numbers in and then gives the total for each array.

So far i've come up with this :
Code:

```#include <stdlib.h> #include <stdio.h> #define NUMBER_ELEMENTS 10 void f_enter (); void f_display (); void main () { float number[NUMBER_ELEMENTS]; f_enter (number, NUMBER_ELEMENTS); f_display (number, NUMBER_ELEMENTS) ; exit(0); } void f_enter (int input[], int size) {         int i = 0 ;         for (i = 0; i < 10; i ++)         {                 printf("Enter number %d for first array: ", i + 1);                 scanf("%d", &input[i]);                 fflush(stdin);         } } void f_display (int display[], int number_elements) {         int i = 0, total = 0 ;         for (i = 0; i < 10; i ++)         {                 total = total + display[i];         }         printf("total is %d ", total); }```
This does it for one array, but I really just copied some other code from a book that did something similar and it somehow works. I really don't see how the array is being passed and used in the display function, or how I would do this for a second array.

Is there anyone who can help explain whats going on in the code and how its being passed between f_enter and f_display.

Renvaar, I'll just ask you to recall that the nicest coaches, are not the best coaches. Sometimes being "stood tall", is a very good thing - although it may not feel like it at that exact moment.

Arrays are continuous memory addresses. The name of the array always gives us the address of the first element of the array - number has exactly the same address as number[0], and that address can not be changed.

Now, for a function to work on the array, it has to have access to the array - that's clear, right? The mechanic can't fix your car, until he gets together with your car, somehow.

Instead of moving every array (and many times arrays are very large), into each function that needs it, we can just send the function the address of the array - it's very efficient.

Whenever your program calls a function, it can send several parameters to that function. In your case, the program sends the name of the array "number", which is the address of the base of the array.

An extra twist is that every function parameter can be given an alias. It's like you joined the French Foreign Legion - you can change your name right away. That's what your program does in f_enter(). It's the same array, the same address as number[], but it has an alias for a name.

Which will make no sense to you now, but for debugging big programs, it's *VERY* helpful.

The squared brackets after the array name, are OK because it's the only pair of brackets for that array. If number was a 2 dimension array: number[][], then the compiler would throw an error.

If you want to see that number is really just an address being passed, replace int number[], with int *number (in the parameter list), and see if it compiles OK or not. (it will).

In this function you have something we don't like - a "magic number". Sometimes they're OK, but usually, they goof up a program, as soon as you change something. Here, looking at the code, I'd see the define statement at the top of the code, and say "OK, I need to change the define to 20, and all will be well.

But it won't work right, now! :( Because of the magic number 10, in this function:

Code:

```void f_enter (int input[], int size) {         int i = 0 ;         for (i = 0; i < 10; i ++)         {                 printf("Enter number %d for first array: ", i + 1);                 scanf("%d", &input[i]);                 fflush(stdin);         } }```
Where did this 10 come from? It just arrived out of thin air, like magic! :p

What we want is i < NUMBER_ELEMENTS, instead of i < magic number 10, or i < size, but NOT i < 10.

Having said that, I use magic numbers in programs, myself. Like right now, I'm programming a chess problem, and I use 64 for the number of squares, frequently. But I'm pretty certain that the classic chessboard will continue to have 64 squares for another few hundred years, see?

Numbers that are subject to change, should never be magic numbers.

fflush(stdin), will let you down - even my old turbo C, which was made to use fflush(stdin), very well, doesn't handle it correctly. Since scanf() skips over whitespace in the input stream (the keyboard buffer in this case), you shouldn't need it, anyway.

And yes, int main() is correct, and we strongly advise you to use it, always. It may not mean anything to you now, but later, if you continue to program, it will - and you'll be glad you learned good programming habits, right from the start.
• 11-19-2009
quzah
Quote: