Download presentation
Presentation is loading. Please wait.
1
Introduction: C Pointers Day 2 These Slides NOT From Text.
2
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!
3
Constant Declaration (cont.) The keyword const indicates a variable that doesn’t change.
4
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, =
5
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.
6
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.
7
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 */ …
8
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.
9
Conditional Compilation You can use a pre-processor statement to do this easily. #ifdef _RAY_01 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 */ #endif None of this will be compiled into your program because you haven’t ever defined a shell variable called _RAY_01
10
Conditional Compilation Want to quickly turn on all the segments so marked? Simply define _RAY_O1. #ifdef _RAY_01 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 */ #endif #define _RAY_01 All this will be part of your program. And, any other section marked with _RAY_01.
11
Other Base Constants Base 10Base 8Base 16 6060x6 90110x9 150170xF
12
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.
13
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.
14
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.
15
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.
16
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.
17
variable = other_variable ++; Main effect, assign the value of other_variable to variable. Side effect, increment other_variable.
18
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!
19
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.
20
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.
21
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)
22
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.h gcc –c lab5.c get_problem.o: get_problem.c proto.h gcc –c get_problem.c …
23
Lab5: Sample Input File 1 150 15 2 8 3 0.6 70 1 4 1000 25 5
24
Lab5: Sample Input File 1 150 15 2 8 3 0.6 70 1 4 1000 25 5 Problem 1 Problem 2 Problem 3 Problem 4 Sentinel -> quit!
25
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 MyOutput MyOutput will NOT have the responses to the input prompts, just the prompts all run together.
26
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.