
|
Pointers and dynamic memorySorry about not updating the course web site for the previous day.
The page for that day is now up on the calendar (if it's still any use).
First of all, your midterm will be given back today. We will go over
midterm solutions as the first thing tomorrow.
How to use pointers
How to use pointers in a program? As your textbook mentions, C defines two operators that
let you manipulate pointer values. They are the ampersand, & operator, and the
asterisk, * operator. & means address-of, and * means value-pointed-to. This is also
in your textbook. I will explain today what they mean. We will discuss this problem,
from your textbook p.484:
double d1; double d2; double *dp1; double *dp2; dp1 = &d1; dp2 = &d2; *dp1 = 3.14159; d2 = 2.71828; dp1 = dp2; *dp1 -= d2; We will discuss how to pass in pointers into functions, so that the functions
can actually modify variables from main. This is called pass-by-reference. We'll
look at this example:
void swap(int* p1, int* p2) {
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
}
int main() {
int a, b;
int* p;
int* q;
p = &a;
q = &b;
a = 10;
b = 5;
swap(p, q);
printf("%d %d\n", a, b);
// prints out 5 10
swap(&a, &b);
printf("%d %d\n", a, b);
// prints out 10 5
return 0;
}Classwork program #1
Make a program that adds days to a calendar date. Your
main function has 3 variables: month, day, year. Let’s say
they contain 12, 30, 2003 respectively. So your date is
12/30/2003. And you ask the user for how many days you
want to add. If the user types in 5, the program has to print
1/4/2004. Since you are writing a function that "converts"
the date, your function will have to modify all 3 variables.
Remember to take care of leap years!
Your function should take in 1 integer (the # of days to add),
and 3 integer pointers!
Dynamic memory allocation
Pointers can be used to create arrays. You do this via dynamic memory allocation. The
function malloc() will help you in this purpose. malloc() takes in an integer as its
arguments, which is the number of bytes to allocate. It returns an address, and thus
you should set a variable of type int* (or char* or any other data types with *) as
the lvalue for the malloc() function.
An example code:
int* someArray; someArray = malloc(10 * sizeof(int)); // allocate memory for an array with 10 elements Classwork program #2
You are to write a program that finds the grade of all
students in a class.
Your program first asks the user to type in how many people are in the class. After that, it asks for each student’s midterm score percentage. After that, it asks for each student’s final score percentage. Midterm and final are each 50% of the grade. How many students are in the class? 10 Student 1 midterm: 60 Student 2 midterm: 75 .. Student 1 final: 90 Student 2 final: 95 .. Student 1 grade: 75% Student 2 grade: 85% .. Homework assignment
For homework, finish the two classwork programs if you did not finish them
in class. Also, read chapter 16 on structs (or structures, or records).
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||