Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.

Similar presentations


Presentation on theme: "CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures."— Presentation transcript:

1 CS100A, Fall 1997, Lecture 241 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

2 CS100A, Fall 1997, Lecture 242 Pointer Parameters (review) Example: Write a function that inter- changes 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.

3 CS100A, Fall 1997, Lecture 243 Example main program: #include void main(void) { int j = 17; int k = 42; swap (&j, &k); printf(“j = %d, k = %d\n”, j, k); }

4 CS100A, Fall 1997, Lecture 244 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 {/* simple structure */ int a;/* integer field */ char ch;/* character field */ } xyz; 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.

5 CS100A, Fall 1997, Lecture 245 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 { /* type definition */ int a; /* int field */ char ch; /* char field */ } Pair; /* type name */ Pair abc; /* 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).

6 CS100A, Fall 1997, Lecture 246 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:

7 CS100A, Fall 1997, Lecture 247 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:

8 CS100A, Fall 1997, Lecture 248 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.

9 CS100A, Fall 1997, Lecture 249 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 low- level — 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 Pair *p; /* p is a pointer to a Pair */ p = (Pair *) malloc( sizeof(Pair) ); p->a = 17; … free(p);

10 CS100A, Fall 1997, Lecture 2410 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.

11 CS100A, Fall 1997, Lecture 2411 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 = ‘!’;

12 CS100A, Fall 1997, Lecture 2412 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.

13 CS100A, Fall 1997, Lecture 2413 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); }

14 CS100A, Fall 1997, Lecture 2414 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); }

15 CS100A, Fall 1997, Lecture 2415 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); }

16 CS100A, Fall 1997, Lecture 2416 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); }

17 CS100A, Fall 1997, Lecture 2417 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: 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 contains routines to copy string values, combine them (like “+” in Java), compare them, etc. hello\0


Download ppt "CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures."

Similar presentations


Ads by Google