Benefit from passing int by reference or by value?

  • Is there a performance advantage by passing ints by reference rather than value? I say this because if you pass by reference you are creating a 4 byte pointer, but if you loop over the value you will still create a 4 byte copy of the value. So they both take up an extra 4 bytes, right?
  • Is it possible to pass an int literal by reference using cast: (int *)? Or do you need to pass a pointer to int? See sample code below:

    int func1(int *a){
        (*a)++; // edited from comment by Joachim Pileborg
        printf("%i\n", *a);
        return 0;
    int func2(int a){
        printf("%i\n", a);
        return 0;
    int main(void){
        func1(&(int *)5); // an int literal passed by reference using a cast ?
        return 0;


source to share

1 answer

The advantage of a step-by-step pointer (there are no references in C) is that the function can update the original int

, that is, return a value to it. No performance benefit; rather, a missed pointer can slow down your program, because the pointer int

it points to must be in address memory, so it cannot be in a register.

Please note that &(int *)5

does not do what you think. (int *)5

assigns a value to a 5

pointer, interpreting it as a memory address. &

will give that pointer address, except that accessing a temporary address is illegal. You probably meant

int i = 5;




All Articles