Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 261 - Winter 2011 Further Introduction to the C programming language.

Similar presentations


Presentation on theme: "CS 261 - Winter 2011 Further Introduction to the C programming language."— Presentation transcript:

1 CS 261 - Winter 2011 Further Introduction to the C programming language

2 Why C ? C is simple language, makes it easier to focus on important concepts Java is high level language, C is low level, important you learn both Lays groundwork for many other langauges

3 Comparing Java and C No classes, inheritance, or polymorphism Functions are the major mechanism for encapsulation Arrays and structures are the major data storage mechanisms Pointers!! Lots of pointers.

4 Function definitions Functions look a lot like methods you are used to in Java, but are not part of a class return-type function-name (arguments) { declarations; /* must come first!! */ function-body; }

5 Variables and Declarations When you declare a variable, space is set aside somewhere in memory for the bits that hold the variable representation. int i; double d; struct arrayBag b;

6 Wait a moment, what is a struct? A struct is like a class with no methods, just data fields. A bundle of data struct arrayBag { int count; EleType data[100]; };

7 Wait a moment, what is EleType? EleType is a symbolic name we will use to represent the type of value we will store in a container. It is defined by a symbolic constant. # define EleType double

8 How big is a struct? As large as the combined data fields. printf(“size of arrayBag %d\n”, sizeof(struct ArrayBag)); Produces “size of arrayBag 808”

9 What the heck is sizeof? Sizeof is a pseduo-function that takes as argument a TYPE, and tells you how many bytes instances of the type need. printf(“size of arrayBag %d\n”, sizeof(struct ArrayBag));

10 Then what the heck is printf? printf is a simple printing function. Takes a formatting string and a list of values %d print integers %f %g print floats and doubles %c print characters %s print strings % print a percent sign

11 Two level scope There are two levels of scope Variables declared outside of any function are global (use sparingly) Variables declared inside of function are local. Declarations must be listed first, before statements. You end up passing more inforation in arguments than you do in Java

12 Parameters are by-value Arguments to functions are passed by value Means the parameter is initialized with a COPY of the argument Will simulate by-reference using pointers

13 Declaring a structure When you declare a variable you must use the struct keyword struct arrayBag foo; No constructors, no initialization, you must use explicit initialization routine

14 Access to data fields Access to data fields uses the same dot notation you are used to: struct arrayBag myData; myData.count = 3; (but often combined with pointers…)

15 Pointers Pointers in C are explicit (implicit in Java) A pointer is simply a value that can refer to another location in memory. 42 A valueA pointer

16 Pointer values vs. thing pointed to Important to distinguish between the value of the pointer and the value of the thing the pointer points to 42

17 Common Example Struct arrayBag myBag; initBag(&myBag); addBag(&myBag, 3.1) addBag(&myBag, 2.7); removeBag(&myBag, 3.1); Printf(“bag now has %d elements”, bagSize(&myBag));

18 Structures and arguments Remember, arguments are passed by value If you pass a structure, the ENTIRE structure is copied into the space for the actual argument value Wasteful, therefore we use pointers are arguments

19 What’s with the & ?? Need to declare a structure. Have to have space someplace Passing a structure is inefficient (parameters passed by value, remember?) Pass a pointer, faster, changes inside the function reflected outside.

20 Inside the function void initBag (struct arrayBag * b) { b->count = 0; } …. Struct arrayBag myBag; initBag (&myBag);

21 What is happening? Count EleType data[100]

22 What is with the arrow? The arrow syntax ( -> ) is a shorthand for two actions - dereference the pointer, and access a field. It says, give me the named field in the thing I’m pointing to b->count = 0; /* same as (*b).count */

23 Structures and by-ref params Very common idiom: struct arrayBag a; /* note, value, not ptr*/ arrayBagInit (&a); /* pass by ref */ arrayBagAdd (&a, 3.14159);

24 Lets finish yesterdays worksheet void addBag (struct arrayBag * b, EleType v) { /* add a new element to the bag */ assert(b->count < 100); b->data[b->count] = v; b->count++; }

25 Test a value Int testBag (struct arrayBag * b, EleType v) { /* see if a value is in the bag */ int i; for (i = 0; i count; i++) { if (EQ(v, b->data[i])) return 1; } return 0; }

26 What a moment, what is EQ? A macro (simple function) used to define equal testing. Makes it easy to change later. # define EQ(a, b) (a == b)

27 What about remove? Can’t have an “empty” location in our bag. Since bags are not ordered, could either slide things down or just copy last value. Lets do latter now, will see former in a few moments.

28 remove Void removeBag (struct arrayBag * b, EleType v) { /* find and remove first occurrence of v */ int i; for (i = 0; i count; i++) { if (EQ(v, b->data[i])) { b->data[i] = b->data[b->count-1]; b->count--; return; /* break out of loop */ }

29 Questions What if v occurs more than once in the bag? What if v is found in the last location? What is the big-Oh running time of remove?

30 Bigger Questions What if our problem requires a collection that only needs to hold five values? What if our problem requires a collection that needs to hold 200 values? What is we don’t know the size of the collection we will need? Leads to next topic


Download ppt "CS 261 - Winter 2011 Further Introduction to the C programming language."

Similar presentations


Ads by Google