# Arrays and functions? (easy question)

• 03-09-2012
Lej1
Arrays and functions? (easy question)
Code:

```#include "stdafx.h" #define size 100 int countPrimes(int,int);   int main() {         /* Declaration of Variables */         int arr[size], i, j=1, remainder, primecount=0, max=size; /* Fill the array with numbers 1 through the max size (100) */     for(i = 0;i < size; i++)     {       arr[i] = j++;     }       for(j = 2; j <= size; j++)         {         for(i = j; i < size; i++)                 {           remainder = arr[i] % j;   /* If the number is not prime, set it to a 0. */                   if(remainder == 0)                         arr[i] = 0;                                 }     } /* Count the prime numbers in the array */                 for(int i=0;i<size;i++){                 if (arr[i]!=0)                         primecount++;         } /* Print the array, not including the 0s. */     for(i = 0; i < size; i++)     {         if (arr[i]!=0)                 printf("%d ",arr[i]);     } /* Print the amount of prime numbers calculated. */                 printf("\nThere are %d prime numbers up to %d.\n\n",primecount,max);         return 0;  }```
I want to use functions for main, instead of doing everything inside of main.. I don't know how to pass arrays though... I want to just have like the functions, "arrayFiller", "primeChecker", "primeCounter", "arrayPrinter".... But how?
• 03-09-2012
quzah
Arrays should be passed using their name, and their size:
Code:

```void foo( type arrayname[SIZE] ) { } ... type anarray[SIZE]; ... foo( anarray );```
So if we flesh that out a bit further:
Code:

```void foo( int a[5] ) {     ... } ... int myarray[5]; ... foo( myarray );```
Now optionally you can omit the left most dimension:
Code:

```void bar( char c[][5] ) {     ... } char a[ 10 ][ 5 ]; ... bar( a );```
This includes single dimension arrays:
Code:

```void bar( int a[] ) {     ... } ... int array[ 1000 ]; ... foo( array );```

Quzah.
• 03-10-2012
Lej1
Nice!, so how do I "return" an array? Got everything else working.

Code:

```#include "stdafx.h" #define size 100 int calcPrimes(int arr[size]); int countPrimes(int arr[size]); void printPrimes(int arr[size],int,int); int main() {         /* Declaration of Variables */         int arr[size],primecount;         arr[size]=calcPrimes(arr);         primecount=countPrimes(arr);         printPrimes(arr,primecount,size); return 0; } /* Function to fill the array and calculate primes */ int calcArray(int arr[size]){         int j=1,i,remainder;     for(i = 0;i < size; i++)     {       arr[i] = j++;     }         for(j = 2; j <= size; j++)         {                 for(i = j; i < size; i++)                 {                   remainder = arr[i] % j;                   if(remainder == 0)                     arr[i] = 0;                 }     }  return arr;  } /* Count the prime numbers in the array */ int countPrimes(int arr[size]){                                 int primecount=0;                 for(int i=0;i<size;i++){                 if (arr[i]!=0)                         primecount++;         } return primecount; } /* Print the amount of prime numbers calculated. */ void printPrimes(int arr[size], int primecount, int max){     for(int i = 0; i < size; i++)     {         if (arr[i]!=0)                 printf("%d ",arr[i]);     }                 printf("\nThere are %d prime numbers up to %d.\n\n",primecount,max);         }```

Currently getting error message: "error C2440: 'return' : cannot convert from 'int []' to 'int'" What does my function type need to be?
• 03-10-2012
TheBigH
You don't need to return anything at all. What you're actually passing to the calcArray function is a pointer to the beginning of the array, together with the size of the array.

That means that when you modify the contents of the array, you're modifying the original rather than a copy as you might expect.
• 03-10-2012
Lej1
Ah! makes sense. So I've changed the function type to void, but it's still not working.

Code:

```#include "stdafx.h" #define size 100 void calcPrimes(int arr[size]); int  countPrimes(int arr[size]); void printPrimes(int arr[size],int,int); int main() {     /* Declaration of Variables */     int arr[size],primecount;     calcPrimes(arr);     primecount=countPrimes(arr);     printPrimes(arr,primecount,size); return 0; } /* Function to fill the array and calculate primes */ void calcArray(int arr[size]){     int j=1,i,remainder;     for(i = 0;i < size; i++)     {       arr[i] = j++;     }     for(j = 2; j <= size; j++)     {         for(i = j; i < size; i++)         {           remainder = arr[i] % j;           if(remainder == 0)             arr[i] = 0;         }     }  } /* Count the prime numbers in the array */ int countPrimes(int arr[size]){                 int primecount=0;         for(int i=0;i<size;i++){         if (arr[i]!=0)             primecount++;     } return primecount; } /* Print the amount of prime numbers calculated. */ void printPrimes(int arr[size], int primecount, int max){     for(int i = 0; i < size; i++)     {     if (arr[i]!=0)         printf("%d ",arr[i]);     }         printf("\nThere are %d prime numbers up to %d.\n\n",primecount,max);     }```

"error LNK2019: unresolved external symbol "void __cdecl calcPrimes(int * const)" (?calcPrimes@@YAXQAH@Z) referenced in function _main"

EDIT: Oh wow.. Nevermind. I see what I did there.. it was a tough one to spot ;)
• 03-10-2012
rags_to_riches
If you ever get this error:

Quote:

error LNK2019: unresolved external symbol
It means you told the compiler you will be providing something (usually via a function prototype), but you either did not provide that (usually a function) at all, or how you provided it differs from what you told the compiler you'd provide. That could be because of a misspelling/typo, or different arguments or return type.

You know the problem, but to clarify for future generations. You told the compiler here:

Code:

`void calcPrimes(int arr[size]);`
"I am going to provide a function later called calcPrimes which takes a pointer to an integer (because that's how an array is seen by the compiler here) and returns nothing"

When the compiler gets here:

Code:

`calcPrimes(arr);`
it says, "OK, this was prototyped. The linker will provide the actual function, so I'll just stick a stub here for the linker to find and insert the function definition"

So the program compiles OK. But then the linking step occurs, the linker finds this stub for a function as prototyped, and can't find it (because you named it calcArray), and you get this linker error.

For more info, Compiling and Linking - Cprogramming.com and An Introduction to GCC - An overview of the compilation process