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

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-03-05T12:58

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building