![Page 1: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/1.jpg)
Introduction: C Pointers
Day 2These Slides NOT From Text.
![Page 2: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/2.jpg)
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!
![Page 3: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/3.jpg)
Constant Declaration (cont.)
The keyword const indicates a variable that doesn’t change.
![Page 4: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/4.jpg)
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, =
![Page 5: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/5.jpg)
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.
![Page 6: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/6.jpg)
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.
![Page 7: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/7.jpg)
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 */…
![Page 8: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/8.jpg)
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.
![Page 9: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/9.jpg)
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
![Page 10: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/10.jpg)
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.
![Page 11: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/11.jpg)
Other Base Constants
Base 10 Base 8 Base 16
6 06 0x6
9 011 0x9
15 017 0xF
![Page 12: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/12.jpg)
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.
![Page 13: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/13.jpg)
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.
![Page 14: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/14.jpg)
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.
![Page 15: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/15.jpg)
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.
![Page 16: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/16.jpg)
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.
![Page 17: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/17.jpg)
variable = other_variable ++;
Main effect, assign the value of other_variable to variable.
Side effect, increment other_variable.
![Page 18: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/18.jpg)
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!
![Page 19: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/19.jpg)
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.
![Page 20: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/20.jpg)
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.
![Page 21: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/21.jpg)
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)
![Page 22: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/22.jpg)
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
…
![Page 23: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/23.jpg)
Lab5: Sample Input File
1150152830.670141000255
![Page 24: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/24.jpg)
Lab5: Sample Input File
1150152830.670141000255
Problem 1
Problem 2
Problem 3
Problem 4
Sentinel -> quit!
![Page 25: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/25.jpg)
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.
![Page 26: Introduction: C Pointers Day 2 These Slides NOT From Text](https://reader036.vdocument.in/reader036/viewer/2022081514/56649d415503460f94a1bc6a/html5/thumbnails/26.jpg)
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.