CS156

CS156: Intro to C, Part I

Spring 2018

Reference

See this page as a slide show

CS156 Reference: Passing Arguments by Reference

Passing by Reference instead of by value

Passing by Reference, not by value

/* Pass by value */
void doStuff(int val) {
    val = 3;
}
int main( ) {
    int x = 5;
    doStuff(x);
    printf("After, x=%d\n", x);
    return 0;
}
After, x=5
/* Pass by reference */
void doStuff(int *val) {
    *val = 3;
}
int main( ) {
    int x = 5;
    doStuff(&x);
    printf("After, x=%d\n", x);
    return 0;
}
After, x=3

Modified: 2016-07-26T18:43

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building