user2809114
user2809114

Reputation: 97

Pass Array by Reference in C

I am trying to pass my array by reference so that I can get around the fact that I cant return an array from a function. I believe I am following the examples that I have seen correctly. Whats going wrong, though? I am expecting the output to be the numbers 0 - 9, but rather I am getting something strange.

Currently my program does not do what is says. It doesn't do any calculations, its only currently testing the pass by reference. I want to make sure that I know how to manipulate the array before I start trying any calculations.

What I am trying to do: create an array of the numbers 0-9, pass it by reference into a function, print each element within the function.

The actual program will create an empty array, send it to the function, then the function will fill the array with prime numbers.

#include <stdio.h>

void primesUpTo(int max,int *resPt);
void main()
{
    const int MAX = 10;
    int primes[MAX];

    /*TEST*/for(int i = 0; i < MAX;primes[i] = ++i)

    primesUpTo(MAX,&primes);    
}

/* Primes up to function
 * Fills array that *resPt points to with primes from 0 to 'max'
 *  *resPt: pointer to first element of result array
 *  max: int, highest prime number in result array
 *  
 *  *resPt must be passed as reference
 */
void primesUpTo(int max, int *resPt)
{
    /*TEST*/for(int i = 0; i < max; i++)
        printf("\nTEST: %d",*(resPt + i)); 
}

My compile error and then output:

steve@steve-VirtualBox:~/C_Programs/CIS/hw_2/ex_3$ gcc -o test printPrimes.c
printPrimes.c: In function ‘main’:
printPrimes.c:11:17: warning: passing argument 2 of ‘primesUpTo’ from incompatible pointer type [-Wincompatible-pointer-types]
  primesUpTo(MAX,&primes); 
                 ^
printPrimes.c:3:6: note: expected ‘int *’ but argument is of type ‘int (*)[(sizetype)MAX]’
 void primesUpTo(int max,int *resPt);
      ^
steve@steve-VirtualBox:~/C_Programs/CIS/hw_2/ex_3$ ./test

TEST: 1835627636
TEST: 1600061541
TEST: 1869833334
TEST: 1952802655
TEST: 0
TEST: 0
TEST: 0
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 1869833334
TEST: 1952802655
TEST: 0
TEST: 0
TEST: 0
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 1952802655
TEST: 0
TEST: 0
TEST: 0
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 0
TEST: 0
TEST: 0
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 4
TEST: 0
TEST: 0
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 4
TEST: 5
TEST: 0
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 4
TEST: 5
TEST: 6
TEST: 0
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 4
TEST: 5
TEST: 6
TEST: 7
TEST: 1
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 4
TEST: 5
TEST: 6
TEST: 7
TEST: 8
TEST: 0
TEST: 1835627636
TEST: 1
TEST: 2
TEST: 3
TEST: 4
TEST: 5
TEST: 6
TEST: 7
TEST: 8
TEST: 9

Upvotes: 1

Views: 7027

Answers (1)

Varun Sharma
Varun Sharma

Reputation: 105

Arrays are always passed by reference in C. The name of the array is pointer to the first element of it. So, you just do this :-

void function (int arr[]){
// Some Code.....
}
int main(){
// Some Code...
int name[5];
function(name);
// Some Code...
}

And that would work, you can modify the values of elements in the array and the changes would be reflected in the calling function.

Edit: You know that you have to add a semi-colon after your for loop? Otherwise the next one line will also be iterated. See -

Corrected Code and Output

Upvotes: 5

Related Questions