Pointers

Content

We start with some simple examples on pointers. Recall:

Example: Modify a variable indirectly

int
main()
{
    int  i = 3;
    int *j = &i;

    *j  = 5;

    return i;
}

Exercise:

Example: Call by reference

In the following example function foo is supposed to change it argument to value 5. An experienced Fortran programmer (but new to C) tries the following:

int
printf(const char *, ...);

void
foo(int i)
{
    i = 5;
}

int
main()
{
    int  i = 3;

    printf("i = %d\n", i);

    foo(i);

    printf("i = %d\n", i);
}