Introduction: C Pointers - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction: C Pointers

Description:

Up to now we have used #define to define a named constant. This depends on the pre-processor. ... The keyword const indicates a variable that doesn't change. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 27
Provided by: csMon
Category:

less

Transcript and Presenter's Notes

Title: Introduction: C Pointers


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
    doesnt 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

i12 / 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
    doesnt work.

/ i12 / 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 i12 / 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 havent 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.

define _RAY_01
ifdef _RAY_01 i12 / 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
All this will be part of your program. And, any
other section marked with _RAY_01.
11
Other Base Constants
Base 10 Base 8 Base 16
6 06 0x6
9 011 0x9
15 017 0xF
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 Cs 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) x0
  • 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
  • Youve 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

OBJSlab5.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 -gt quit!
25
Lab5 Sample output
  • You test your lab by running it from the screen
    and keyboard.
  • Then, when its working, redirect BOTH input and
    output.
  • lab5 lt MyInput gt 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.
Write a Comment
User Comments (0)
About PowerShow.com