Transcript (ppt)

CS100A, Fall 1997
Lecture 24, Tuesday 25 November
(There were no written notes for lecture 23 on Nov. 20.)
Data Structures in C
Review:
• C Pointers and parameters
Concepts:
• C structures (struct)
• Type definitions (typedef)
• C arrays
• Struct and array function parameters
• C strings
CS100A, Fall 1997, Lecture 24
1
Pointer Parameters (review)
• Example: Write a function that interchanges the contents of two variables given
as arguments to the function.
• Function declaration:
void swap (int * x, int * y) {
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
Note: There was a typo in this function definition in the
notes from lecture 22. The correct type for the parameters
is “int *” (pointer-to-int). The “int &” typo in the previous
notes is a C++ notation for reference (not pointer)
parameters, which are not part of C.
CS100A, Fall 1997, Lecture 24
2
• Example main program:
#include <stdio.h>
void main(void) {
int j = 17;
int k = 42;
swap (&j, &k);
printf(“j = %d, k = %d\n”, j, k);
}
CS100A, Fall 1997, Lecture 24
3
Structures
• C provides structures to group collections
of variables into a single data object. A
single structure variable may be declared
and used as follows.
struct {
int a;
char ch;
} xyz;
/* simple structure */
/* integer field */
/* character field */
xyz.a = 10;
xyz.ch = ‘q’;
printf(“%d\n”, xyz.a * 3);
• Declaration of a structure variable in C
actually creates the structure. It does not
need to be separately allocated using
something equivalent to new (as in Java).
• Structure variables may be local to a
function, just like ints, chars, and doubles.
CS100A, Fall 1997, Lecture 24
4
Type Definitions
• Structures are normally not defined in
variable declarations. Instead, they usually
appear in type definitions that give a name
to the structure. That name can then be
used to declare variables and parameters
with that structure type.
typedef struct {
int a;
char ch;
} Pair;
Pair abc;
/* type definition */
/* int field */
/* char field */
/* type name */
/* variable declaration */
abc.a = 17;
• A C struct type is similar to a Java class, but
a struct may only have data members (no
methods or functions).
CS100A, Fall 1997, Lecture 24
5
Struct Parameters
• Structs in C behave exactly like ints, chars,
and other simple types when used as
parameters. If pointers aren’t used, the
value of the struct argument is copied.
typedef struct {…} Pair; /*as before*/
void fv (Pair p) {
p.a = 42;
}
void main(void) {
Pair pr;
pr.a = 17; pr.ch = ‘?’;
fv(pr);
printf(“%d\n”, pr.a);
}
Output:
CS100A, Fall 1997, Lecture 24
6
Struct Pointer Parameters
• If the function needs to be able to access the
actual argument and change it, pointers can
be used.
void fr (Pair *p) {
(*p).a = 42;
}
void main(void) {
Pair pr;
pr.a = 17; pr.ch = ‘?’;
fr(&pr);
printf(“%d\n”, pr.a);
}
Output:
CS100A, Fall 1997, Lecture 24
7
The -> Operator
• Pointers to structures are frequently used as
parameters, which means the operation of
dereferencing a pointer and selecting a field
is quite common. C provides the ->
operator as a convenient shorthand.
Function fr would usually be written:
void fr (Pair *p) {
p->a = 42;
}
• Note: In Java, all objects (structs) are
allocated dynamically, and “object-valued”
variables are references to objects, not the
objects themselves. Java’s “p.a” notation is
roughly equivalent to p->a or (*p).a in C.
CS100A, Fall 1997, Lecture 24
8
Dynamic Storage Allocation in C
(Only for those who are curious)
• The standard library function malloc is used
to obtain a block of storage. It is very lowlevel — its argument is the number of bytes
or words to be allocated (typically obtained
with the sizeof operator). It returns an
untyped pointer to the allocated area, which
must be cast to the desired type.
• Dynamic storage must be explicitly released
by calling function free. No automatic
garbage collection is provided.
#include <stdlib.h>
Pair *p;
/* p is a pointer to a Pair */
p = (Pair *) malloc( sizeof(Pair) );
p->a = 17;
…
free(p);
CS100A, Fall 1997, Lecture 24
9
•
•
•
•
•
•
C Arrays
C arrays are syntactically similar to arrays
in Java, but the semantics are different.
Declaring an array in C actually allocates it.
Declaring an array of structs allocates a
block of storage with space for all of the
structs in the array.
Array variables may be local to a function
(as with ints, doubles, structs, etc.).
Array indices start at 0.
There is no built-in operation to determine
the length of an array.
CS100A, Fall 1997, Lecture 24
10
Examples
int b[10]
/* creates b[0]…b[9] */
Pair v[10]
/* creates v[0]…v[9] */
int k;
for (k = 0; k < 10; k++)
b[k] = 0;
v[0].a = 10;
v[2].ch = ‘!’;
CS100A, Fall 1997, Lecture 24
11
•
•
•
•
C Array Parameters
An array name in C is, effectively, a pointer
to the first element of the array. Therefore,
When an array name is used as a parameter,
the “value” of the parameter is a pointer to
its first element. Therefore,
Array parameters are always pointers. The
called function refers to the argument array
directly. No copy of the array is made. So,
In a function call argument list, an &
operator is usually not needed or
appropriate for an array parameter.
CS100A, Fall 1997, Lecture 24
12
C Array Parameter Example
/* set a[0..n-1] to 0.0 */
void zero (double a[ ], int n) {
int k;
for (k = 0; k < n; k++)
a[k] = 0.0;
}
void main(void) {
double b[100];
zero(b, 100);
}
CS100A, Fall 1997, Lecture 24
13
C Array Parameter Example, Continued
(Technical details for the curious.)
• Because array names are treated as pointers,
C programmers tend to use pointers and
arrays somewhat interchangeably. Function
zero could have been written with a pointer
parameter. It could still be called with an
array name as the argument.
/* set a[0..n-1] to 0.0 */
void zero (double *a, int n) {
int k;
for (k = 0; k < n; k++)
a[k] = 0.0;
}
void main(void) {
double b[100];
zero(b, 100);
}
CS100A, Fall 1997, Lecture 24
14
Arrays of Structures as Parameters
• An array of structs is treated like any other
array. If a parameter is an array of structs, a
pointer to the beginning of an array (i.e., an
array name) is the appropriate argument.
/* store 0’s and blanks in pa[0..n-1] */
function zap (Pair pa[ ], int n) {
int k;
for (k = 0; k < n; k++)
{pa[k].a = 0; pa[k].ch = ‘ ’; }
void main(void) {
Pair ray[42];
/* zap ray[0..16] */
zap(ray, 17);
}
CS100A, Fall 1997, Lecture 24
15
Structs Containing Arrays as Parameters
• Structs may contain arrays, but they still
behave as structs. A struct argument will be
copied unless pointers are used.
typedef struct {
int a[10];
} Ray;
void isCopied(Ray r) {
r.a[2] = -1; /* doesn’t change */
}
/* actual argument. */
void notCopied(Ray * r) {
r->a[2] = -1; /* changes argument */
}
void main(void) {
Ray a;
isCopied(a);
notCopied(&a);
}
CS100A, Fall 1997, Lecture 24
16
Strings
• A string in C is an array of characters. The
array contains the characters in the string
followed by an extra char (byte) containing
a binary zero.
• Example:
“hello”
/* string constant */
• Representation:
h
e
l
l
o
\0
• Because strings are arrays, and array names
are basically pointers, the basic assignment
and comparison operations manipulate
pointers, not the strings themselves.
char s[ ] = “hello”;
char t[ ] = s; /*s,t point to same place*/
s[1] = ‘?’
/* s, t are now both “h?llo” */
• The standard library <strlib.h> contains
routines to copy string values, combine
them (like “+” in Java), compare them, etc.
CS100A, Fall 1997, Lecture 24
17