CS 113 Lecture 6: More On Pointers, Arrays, and Strings
- last time we looked at & and *
& x == "the address of x"
- can be applied to any variable
- cannot be applied to a constant
- must be an object in memory
*xptr == "the thing pointed to by xptr"
- can be applied to any pointer
- the major application was passing objects "by reference" to procedures
instead of "by value". This allowed us to change the values of parameters.
- we can now understand why scanf arguments were passed as they were:
int i = 0;
scanf("%d", & i); /* pass the address of i so it can be modified */
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void swap(int *x, int *y)
{
int temp;
temp = x; /* error: "cannot convert (int *) to int */
x = y;
x = temp; /* error: "cannot convert int to (int *) */
return;
}
- there are some places where the compiler will not tell you
that you have made a mistake:
eg
int *increment_pointer(int *x)
{
int temp = 1;
return x + temp;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Another Example: Initializing a struct:
#include < stdio.h>
struct date {
int month, day, year
};
void getDate(struct date *dt)
{
int m, d, y;
scanf("%d %d %d\n", & m, & d, & y);
(*dt).month = m; /* the parens are necessary */
(*dt).day = d;
(*dt).year = y;
return;
}
void printDate(struct date dt)
{
printf("%d %d %d\n", dt.month, dt.day, dt.year);
}
int main(void)
{
struct date dt1;
getDate( & dt1);
printDate(dt1);
return 0;
}
- Draw pictures to show what a struct looks like in memory
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A closer look at Arrays
- let's look at what an array looks like in memory:
int a[6];
- a series of cells in a row(draw picture)
- the name of an array is just a pointer - almost(more on this later)
Arrays as Parameters:
- Example:
/* A must be at least 2 elements long */
void arrayswap(int A[])
{
int temp;
temp = A[1];
A[1] = A[2];
A[2] = temp;
return;
}
int main(void)
{
int a[] = {0,1,2,3,4};
arrayswap(a);
printf("%d, %d\n", a[1], a[2]);
return 0;
}
- the pointer/array name is copied into arrayswap.
- the values inside the array may change because you can reference
them using the pointer. the array values themselves are not copied.
Example: Arrays of Structures
struct employee{
int ssn;
float pay;
};
void emp_swap(struct employee *e1, struct employee *e2)
{
struct employee temp;
temp.ssn = (*e1).ssn;
temp.pay = (*e1).pay;
(*e1).ssn = (*e2).ssn;
(*e1).pay = (*e2).pay;
(*e2).ssn = temp.ssn;
(*e2).pay = temp.pay;
}
/* bigBucks: move all the people making cutoff or more to the front of the
array. All the people making less than cutoff will be put at the
end of the array.
Notice, we include the length of the array in our array procedures
because we can't otherwise find out how big our array is. */
int bigBucks(struct employee emps[], int length, float cutoff)
{
int i, where;
for (i=0, where=0; i < length; i++){
if (employee[i].pay > = cutoff){
emp_swap( & (emps[i]), & (emps[where]));
where++;
}
}
return where;
}
/* Print out all of the people making more than $40 */
#define CUTOFF 40
#define EMP_NUM 3
struct employee emps[EMP_NUM] =
{
{234, 40.00},
{543, 30.00},
{112, 35.00},
{211, 50.00},
{555, 42.00}
};
int main(void){
int where, i;
where = bigBucks(emps, EMP_NUM, CUTOFF);
for (i=0; i < where; i++) {
printf("%d is making %f\n", emps[i].ssn, emps[i].pay);
}
return 0;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Exercises with Pointers
char *cp1, *cp2;
int *ip;
- *ip is an integer
- *cp is a character
- we can use *cp anywhere we could use a regular character.
- cp is a pointer to a character - we cannot use it where we would
use a regular character - we can only assign addresses to it.
char c = 'a';
char *cp;
cp = & c;
printf("%c", *cp);
- more examples:
float a = 3.0, b = -17.0;
float *fp;
fp = & b; /* fp points to b */
b++; /* *fp is now -16.0 */
*fp = *fp - 2.0; /* b is now -18.0 */
a = *fp; /* a is now -18.0 */
a++; /* a is now -17.0 but b and *fp are -18.0 still */
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Assignment #1:
If you didn't receive 2/3 or 3/3 on your assignment, you must correct
it and resubmit it. You will receive the grade I give the
resubmission. You need to have mastered the items on that assignment
before you can do the current assignment.
- some people were confused about printf and scanf.
remember:
scanf's arguments are pointers - it changes its arguments
printf's arguments are NOT pointers - it does not change its arguments
- remember:
- you MUST HAND IN sample output of your programs
- programs which do not compile are not acceptable assignment solutions
- I understand if you had some problems with CodeWarrior
- if you are having a problem: GET HELP
My Office Hours(5139 Upson):
Mon: 1:10-2:40
Thurs: 3:30-4:30
Consultants(320 Upson):
Jiesang: Sunday 3-7
Kyle: Monday 3-5, Tuesday 5-7
Tyler: Monday 5-7, Tuesday 3-5
T-C: Wednesday 6-10
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~