1

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
7
  • Read about array to pointer conversion. Commented Oct 2, 2016 at 21:08
  • Don't pass in &primes, instead pass in just "primes" Commented Oct 2, 2016 at 21:12
  • you need a ';' after for(int i = 0; i < MAX;primes[i] = ++i); else your call to primesUpTo is in the for loop. Commented Oct 2, 2016 at 21:12
  • Also primes[i] = ++i is UB. Commented Oct 2, 2016 at 21:13
  • One suggestion, use always the brackets to delimit both your loops and your code blocks ;) Commented Oct 2, 2016 at 21:15

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

5 Comments

Do I access the array like normal? ex: name[1] = 5;? I changed my program according to your answer but I still have weird output, it looks a lot like the output posted above.
C has only pass-by-value -- neither arrays nor any other arguments are ever passed by reference. In fact, arrays are never passed at all. If a function argument evaluates to an array, then it is converted to a pointer to the first element, and the pointer is passed (by value). Passing a pointer by value has semantics similar to pass by reference, but not identical.
@JohnBollinger In programming the term "pass by reference" means to pass in such a way that the function can make changes and the caller sees those changes. So this method passes the array by reference (and also passes a pointer by value)
JohnBollinger is splitting hairs but he's right. Pass by reference doesn't exist in C. The usual way we put it is that arrays decay into pointers when passed into functions. As usual pointers are passed by value. Example: It's possible for an array parameter to be NULL in C which would not be possible with real pass-by-reference such as in C++.
@user2809114 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.