Pointers review c programming.doc

4 Pages
Unlock Document

Computer Programming and Analysis
Computer Programming and Analysis IPC144
Arta Kogan

IPC144 Week 5 Part 2 Agenda: 1 More pointers 2 Homework More Pointers Pointers can be used to access and modify the local variables from another function. In order to do that the address of the variable is passed to the function. Then the value of the variable is changed inside of the function using its address. The main advantage of using pointers is that a function can return (change) more then one value. Example 1: fun15.c void func(int *p); /* an example of a function prototype – function will receive an address and store it into a pointer p */ int func2(int *p, double *p_num, int another_num); /* an example of the function prototype – function will receive three parameters: an address of an integer – and use the pointer p to store it an address of a double – and use pointer p_num to store it a value of an integer – and an integer another_num to store it */ int main(){ int num, x, z = 54; double y = 5.6; func(&x); /* an example of a function call – sending an address to the function */ num = func2(&x, &y, z); /* Important!! Three parameters are sent: the address of an integer x the address of a double y and a copy of the value of an integer z */ printf(“x = %d, y = %.2lf, z = %d, num is %d\n”, x, y, z, num); return 0; } /* function definitions */ void func(int *p){ *p = 77; } int func2(int *p, double *p_num, int another_num){ *p = another_num; another_num ++; *p_num = 56.34; another_num += *p; return another_num – 5; } Example 2: /* walk12.c */ /* determine the exact output of the following program */ void go_on(int *p, int x); int main(){ int a = 7; int *pointer; pointer = &a;
More Less

Related notes for Computer Programming and Analysis IPC144

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.