2330 : Introduction to Programming in C

June 30th - Pointers

Midterm Exam
Before we start our lecture for today, we would like to announce something you've all been waiting for for a long time =) Your midterm exam will take place next week, on Monday (a week from now, on July 7th). You have a week from now to prepare for it. The exam will be open books and open notes. You may use any of the required/recommended textbooks and any/all of your lecture notes. No talking or group work allowed, and thus no monitors also - as having monitors on could easily allow you to talk to others in the class on AIM or something.
We emphasize on understanding on concepts, not memorizing syntax. That is what CS is about. So don't think you're at a disadvantage if you don't have the recommended books - if you "know your stuff," those won't do anything.

The topic of pointers is no doubt the hardest topic that we will cover/have covered for this class. The basic concepts of pointers take a lot of thinking to understand. This *will* be hard stuff, but we hope to make it not as hell-ish as it is for you...
Assigning values to a variable
Before we begin our discussions on pointers, we will review the simple topic of assigning values to a variable. You may think you know this well, but you will really need to have a 100% clear understanding of this, otherwise when we get into pointers, it will simply be more and more confusing. Understand what is an lvalue, what qualifies as one, and what doesn't. Understand the difference between x = y and y = x. Understand why you cannot do x+y = 5, or 5 = x+y, or 5 = x, or x+y = z, but you can do z = x+y.
What pointers are
A pointer is a kind of data type that lets you store memory addresses in it. Like its name implies, a pointer lets you point to a variable. Detailed explanations will be presented in lecture, make sure you pay full attention during lecture today. I cannot emphasize this more, this is a very confusing topic and you can easily get lost as this is the first time you learn about it.
Why use pointers
These can be found on the first page of the pointers chapter in your textbook, p.454. Each of these will be discussed in the lecture.
  • Pointers allow you to refer to a large data structure. (Data structures will be covered later)
  • Pointers facilitate sharing data between different parts of a program.
  • Pointers make it possible to reserve new memory during program execution.
  • Pointers can be used to record relationships among data items. (advanced, we will not use)
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.

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

Homework assignment
Finish problem #5 on page 487. Review everything covered so far in the course, come prepared with questions to ask us on midterm-review day.