Introduction: C Pointers Day 2 These Slides NOT From Text.

Post on 20-Dec-2015

221 views 1 download

Tags:

Transcript of Introduction: C Pointers Day 2 These Slides NOT From Text.

Introduction: C Pointers

Day 2These Slides NOT From Text.

Constant Declarations

Up to now we have used #define to define a named constant.

This depends on the pre-processor. Another way is to define a constant

inside the regular code. e.g. const float PI = 3.14159; PI = 3.0; is now illegal!

Constant Declaration (cont.)

The keyword const indicates a variable that doesn’t change.

const Restrictions

Constants must be initialized when they are defined.

Constant values can never be changed.

I.E. They can never appear on the left of an assignment operator, =

const vs. #define

C checks the syntax of const statements immediately. The #define directive is not checked until the macro is used.

const uses C syntax, while #define has a syntax all its own.

const follows normal C scope rules, while constants defined by a #define directive continue on forever.

const vs. #define (cont.)

The #define directive can only define simple constants. The const statement can define almost any type of C constant, including things like structures.

The #define directive is essential for things like conditional compilation and other specialized uses.

Conditional Compilation

One use is to “comment out” a bunch of code. Suppose you had:

…i=12;/* This is a one line comment */J = 15; /* This is a comment at the end of a line */K = -10;X = sqrt (102.34);/* Another one line comment */…

Conditional Compilation

You would like to eliminate all this code temporarily. Comment before and after? No, that doesn’t work./*

i=12;/* This is a one line comment */J = 15; /* This is a comment at the end of a line */K = -10;X = sqrt (102.34);/* Another one line comment */*/

Terminates the comment here.

Conditional Compilation

You can use a pre-processor statement to do this easily.

#ifdef _RAY_01i=12;/* This is a one line comment */J = 15; /* This is a comment at the end of a line */K = -10;X = sqrt (102.34);/* Another one line comment */#endif

None of this will be compiled into your program because you haven’t ever defined a shell variable called _RAY_01

Conditional Compilation

Want to quickly turn on all the segments so marked? Simply define _RAY_O1.

#ifdef _RAY_01i=12;/* This is a one line comment */J = 15; /* This is a comment at the end of a line */K = -10;X = sqrt (102.34);/* Another one line comment */#endif

#define _RAY_01

All this will be part of your program. And, any other section marked with _RAY_01.

Other Base Constants

Base 10 Base 8 Base 16

6 06 0x6

9 011 0x9

15 017 0xF

const Pointers

const char * answer_ptr = “Forty_Two”;

Does NOT tell C that the variable answer_ptr is a constant! Instead, it tells C that the data pointed to by answer_ptr is a constant.

The data cannot be changed, but the pointer can.

Pointer Is Constant

If we put the const after the *, we tell C that the pointer is constant.

char * const name_ptr = “Test”; The data can be changed, but the

pointer cannot.

Or Both Unchangeable

To make them both constants, put two const in.

const char * const title_ptr = “Title”;

The data cannot be changed, and the pointer cannot be changed.

But WHY? I said newer programs like Ada and

Java provide automatic protection for the programmer. Similar to guards and safety switches on a table saw.

I also said C was like a spinning blade in space.

Using const carefully can protect your code from inadvertent side effects.

Side Effects? One of the worst habits of people

my age is being proud of their mastery of C’s side effects! Look how clever I am! However, no one can read the code!!!

Read the enrichment puzzles for examples of these.

But, we have looked at some, the increment and decrement operator.

variable = other_variable ++;

Main effect, assign the value of other_variable to variable.

Side effect, increment other_variable.

if(variable = expression) x=0;

Main effect: assign 0 to x if the value of the assignment is true (!= 0).

Side effect: assign the value of expression to variable.

Side effects are sort-of hidden actions taken by the language.

Try to avoid them. If you use them, add comments!

Compiler Options

Depend on the compiler, but many similar ones.

Added on the command line, after the gcc command, before the file names.

gcc {… options … } file_names; You’ve seen a couple:

-c compile only. -o define output run image file name.

Other Useful Compiler Options

-g : insert debugging statements. -Wall : Print all warnings. Provides

cleaner executing code. -E : preprocess only and produce

the results of all preprocessor directives.

Lab #5 Hints Use a separate file

for each function. lab5.c get_problem.c get_rate_drop_factor.

c get_kg_rate_conc.c get_units_conc.c fig_drops_min.c fig_ml_hr.c

by_weight.c by_units.c proto.h

These are small functions.

Some have pass by reference (pointer) parameters. (book calls them output)

Lab5: Intermediate Makefiles

Use Makefile variables:

OBJS=lab5.o get_problem.o get_rate_drop_factor.o \get_kg_rate_conc.o get_units_conc.o fig_drops_min.o \fig_ml_hr.o by_weight.o by_units.o

lab5: $(OBJS)gcc –o lab5 $(OBJS)

Lab5.o: lab5.c proto.hgcc –c lab5.c

get_problem.o: get_problem.c proto.hgcc –c get_problem.c

Lab5: Sample Input File

1150152830.670141000255

Lab5: Sample Input File

1150152830.670141000255

Problem 1

Problem 2

Problem 3

Problem 4

Sentinel -> quit!

Lab5: Sample output You test your lab by running it

from the screen and keyboard. Then, when it’s working, redirect

BOTH input and output. $ lab5 < MyInput > MyOutput MyOutput will NOT have the

responses to the input prompts, just the prompts all run together.

Lab5: Matching Exactly!

A little pain for you. Saves a big pain for us reading 65

submissions! See web page write-up, I added a

sample output which you can easily count blanks.