CS2 - PowerPoint PPT Presentation

1 / 93
About This Presentation
Title:

CS2

Description:

Questions? Preprocessing ... Misc. C Trivia -- Enough already! First there was I/O... In Java we had an easy choice: System.out.println ('Hello World' ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 94
Provided by: davidd1
Category:
Tags: cs2 | easy | questions | trivia

less

Transcript and Presenter's Notes

Title: CS2


1
CS2
  • Introduction to
  • Object Oriented Programming
  • Last C Lecture
  • I/O Misc Topics

2
Now it gets weird...
  • C allows us to have arrays which appear to be
    like other languages
  • int a100
  • creates an array of 100 elements which can be
    referenced as a0 through a99.
  • Note that an array created in this manner is
    truly an array
  • However, we can also do strange and wonderful
    things now like
  • int b
  • b a
  • Note This is the same as b a0

3
Strange and Wonderful Thing I
  • int main(void)
  • int a10
  • int b
  • int i
  • for (i 0 i lt 10 i)
  • ai i
  • b a / What happens here, recall b a0
    /
  • for (i0 i lt 10 i)
  • (bi) (bi) 2 / And here? /
  • printf("d d\n", i, ai)
  • return 0

4
Strange and Wonderful Thing II
  • int main(void)
  • int a10
  • int b
  • int i
  • for (i 0 i lt 10 i)
  • ai i
  • b a
  • for (i0 i lt 10 i)
  • bi bi 2 / What you say? /
  • printf("d d\n", i, ai)
  • return 0

5
Strange and Wonderful Thing III
  • int main(void)
  • int a10
  • int b
  • int i
  • for (i 0 i lt 10 i)
  • ai i
  • b a
  • for (i0 i lt 10 i)
  • ib ib 2
  • / What! /
  • printf("d d\n", i, ai)
  • return 0

6
Caution
  • Many people will say that arrays in C are just
    pointer manipulations -- but an actual array is a
    special thing
  • So while it was okay to say
  • b a
  • It would have been wrong to say
  • a b
  • Once you create a true array
  • int a10
  • You shouldn't try and assign something else to "a"

7
"Actual Array"
  • In C, we can dynamically allocate memory and
    we're going to just show you enough to make you
    incredibly dangerous.
  • DON'T CUT GREENLEE'S LECTURES ON THIS SUBJECT
  • It could be a huge mistake!
  • We are only telling you part of the
    story!!!!!!!!!!!!!!!!!

8
Allocating Memory
  • There is a function in a C library which allows
    you to ask for some memory. You tell it how much.
    It tells you that
  • a. You got it and where it's located
  • OR
  • b. You didn't get it
  • The feared and dreaded
  • void malloc(size_t n)

9
Pseudomemories
  • Remember the new function in Pseudocode?
  • You said current lt- new(Node)
  • So the idea was that the size of the Node was
    looked up somewhere and then enough memory was
    allocated to hold one Node.
  • Malloc works sort of like that only you can ask
    for as much memory as you want.
  • Note that both return pointers.

10
Warning, Danger, Will Robinson
  • This is not a complete treatment of dynamic
    memory management in C.
  • There is no automatic garbage collection in C
  • There is no automatic garbage collection in C
  • There is no automatic garbage collection in C
  • There is no automatic garbage collection in C
  • You must eventually learn
  • Memory allocation
  • Memory reallocation
  • Memory deallocation
  • From Mr. Greenlee - You'll love it, honest! -)

11
Simple Malloc
  • Let's say you want space for 100 ints.
  • Here is what you do
  • int pint
  • pint (int )malloc(100 sizeof(int))
  • if (null pint)
  • / You did not get the space! /
  • / Handle error here -- very important /
  • / Now you have effectively an array of ints /
  • for (i 0 i lt 100 i)
  • pinti 0

12
Going crazy?
  • Did we mention pointers to pointers?
  • We did this before
  • int x
  • int px
  • px x
  • We can also go one (or more) step(s) further
  • int ppx
  • ppx px
  • Which would allow us to do things like
  • ppx 42 / Puts 42 into x /

Ready to run back to Java yet?
13
Visually
  • int x

1000
??
x
2000
??
3000
??
14
Visually
  • int x
  • int px

1000
??
x
2000
??
px
3000
??
15
Visually
  • int x
  • int px
  • int ppx

1000
??
x
2000
??
px
3000
??
ppx
16
Visually
  • int x
  • int px
  • int ppx
  • px x

1000
??
x
2000
1000
px
3000
??
ppx
17
Visually
  • int x
  • int px
  • int ppx
  • px x
  • ppx px

1000
??
x
2000
1000
px
3000
2000
ppx
18
Visually
  • int x
  • int px
  • int ppx
  • px x
  • ppx px
  • ppx 42

1000
42
x
2000
1000
px
3000
2000
ppx
19
Stringing You Along
  • Since we're talking about arrays and pointers and
    pointers to pointers we should briefly mention
    strings
  • Strings in C are implemented as arrays of
    characters
  • So just as you can say
  • int a 0, 1, 3, 5
  • and C will correctly dimension a to be a4
  • You can also say
  • char b 't', 'e', 's', 't', '\0'
  • And you can even say
  • char c "test"

20
Watch this closely
  • char aLine "Hi, there!"
  • char pLine "Hi, there!"
  • These are similar but not the same!!!
  • The first actually creates space for an array of
    characters (11) which can be subsequently
    modified
  • aLine4 'T'
  • The second creates a pointer which is pointing to
    a string constant so that
  • pLine4 'T' would be undefined while
  • pLine aLine
  • pLine4 'T' / is okay /

21
Over the top!
  • It is not only possible to have pointers to
    pointers,
  • but one can even use arrays of pointers!
  • char name "Illegal", "Jan", "Feb", "Mar",
    "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
    "Nov", "Dec"
  • Now for example name2 is a pointer to the
    character string 'F', 'e', 'b', '\0'
  • What would we do with an array of pointers?

22
Imagine
  • We have some names like
  • Smith, David
  • Leahy, Bill
  • Dagon, David
  • Morales IV, Ricky Martin
  • Suppose we want to sort the names but we note
    that they are of different lengths.
  • Instead of running a sort algorithm which swaps
    the character strings around why not create an
    array of pointers

23
We have an array of pointers
Smith, David
name0
Leahy, Bill
name1
Dagon, David
name2
Morales IV, Ricky Martin
name3
24
And we just move the pointers in the array to sort
Smith, David
name0
Leahy, Bill
name1
Dagon, David
name2
Morales IV, Ricky Martin
name3
25
More fun
names
0
  • If we define
  • char names30
  • we get
  • The following are all legal
  • char z
  • names4 z
  • names5 "fred"
  • names6 (char)malloc(20sizeof(char))

1
...
29
Pointers to chars
26
Running with the big dogs
  • The classic data structure in C which is
    equivalent to
  • Records in Pseudocode or Pascal
  • Classes (without methods) in Java
  • Whatever in Scheme (Like a list sort ofor maybe
    a dotted pair)
  • is called a struct
  • They look like this
  • struct ordPair
  • int x
  • int y

27
More structs
  • So we could declare
  • struct ordPair opAlpha, opBeta
  • and access them like this
  • opAlpha.x 7
  • opAlpha.y 8
  • opBeta opAlpha
  • Structs are what we would use for the classic
    linked list node
  • struct LLnode
  • int data
  • struct LLNode next

28
Putting it all together
  • Here is how we could declare a head pointer
  • struct LLnode head
  • here is how we could get a new node dynamically
  • head (struct LLnode )malloc(sizeof(struct
    LLnode))

29
One last thing
  • C allows you to make up your own type identifiers
    using typedef (one more time!)
  • This is used as follows with struct definitions
  • typedef struct Node
  • int data
  • struct Node next
  • Node
  • Now we can say
  • Node head

30
Questions?
31
Preprocessing
Compilation begins by moving the C source files
through a preprocessor--a simple text
substitution device. One can designate strings
for substitution with the expression
define ltexpressiongt ltsubstitutegt
32
A Tale of Two Expansions
The C preprocessor has many features--and
pitfalls. define g(y) get_index(y) g(z) defin
e g (y) get_index (y) g(z)
get_index(z) (y) get_index (y) (z)
33
The Power of Parens
Inputs
Results
define DIVIDE(x, y) x/y DIVIDE(3, 34)
3/34
define DIVIDE(x, y) (x)/(y) DIVIDE (3, 34)
(3)/(34)
34
More Substitution
What's wrong with this?
define square(x) x x
And not (z4) (z4)
35
Common Sense Preprocessing
1
Watch extra white space, as shown in the previous
examples.

2
Liberally use parentheses to protect order of
precedence
36
Questions?
37
And now. I/O in C
Streams -- What are they? Reading from the
keyboard -- safety first More printf fun --
more than meets the eye Reading/writing
files Misc. C Trivia -- Enough already!
38
First there was I/O...
  • ...then computers!

39
Input 1
  • Jacquard uses "punched cards" to control loom
  • Hollerith at MIT develops punched card technology
    to handle 1890 census data
  • CTR (Calculating, Tabulating, and Recording
    Company (1911) ? IBM)
  • IBM (1924)
  • Fixed length records

40
The formerly ubiquitous punched card
41
Input 2
  • Telephone
  • Telegraph
  • Wireless
  • Teletype
  • Paper Tape
  • Paper tape wastes no space. It appears to be a
    stream of data arriving as input or leaving as
    punched output.

42
Streaming Input
  • Paper tape
  • Input from keyboard
  • Input from modem
  • Others?

43
Well, yes...
  • Even devices like disk drives which inherently
    are not "stream" oriented devices can be made to
    act that way through software techniques.

44
By default
  • A C program will have several "streams" defined
    by default.
  • By some remarkable coincidence they are similar
    to the streams in Java (in, out, err)
  • C Defaults
  • stdin
  • stdout
  • stderr

45
stdin, stdout, sterr
  • By default stdin is reading from the keyboard.
  • stdout is writing to the screen
  • stderr is also writing to the screen
  • Why do we have two output streams?
  • In a nutshell...it's possible to "redirect" the
    output streams. (What was done when using dat/ans
    files.)
  • So we can redirect stdout into a file and still
    see error messages sent to stderr
  • Very clever!

46
printf
  • In Java we had an easy choice
  • System.out.println ("Hello World")
  • System.err.println ("Something is terribly
    wrong!")
  • In C the "normal" printf command is by default
    hooked up to just stdout
  • printf ("Hello World\n")

47
Buffers?
  • Whether a device is inherently stream oriented
    (like a keyboard) or being forced to "emulate"
    stream behavior doesn't matter.
  • Modern computer systems store stream data in
    buffers.
  • When you ask for some it gets it out of the
    buffer.
  • This normally solves a speed mismatch problem and
    makes things much more efficient.

Almost anything can be used as a stream, or a
source of input a keyboard, a network
connection, a file--even a string in memory.
48
Aha!
We've already seen (a little) how streaming
objects can be used to incrementally read
information.
BufferedReader br FileReader fr new
FileReader(strFileName) br new
BufferedReader(fr) String strLine null while
( (strLine br.readLine() ) ! null) //
handle new line of input in strLine
Here, the bulk of the file remains resident on
disk, and we use a buffer to read lines from
disk. The stream object is expressed a
"FileReader", wrapped in a BufferedReader for
greater flexibility.
49
Reading from the Keyboard
In C, we can use the putchar and getchar()
methods to move single characters to the console,
from the keyboard. The prototypes for these
methods int getchar(void) int putchar(int
c) These are included in stdio.h, used in just
about every C program. Both of these methods
return "EOF", usually defined at "-1", if there's
an error. Note that getchar returns an int,
which we might cast to a char. Why would anyone
in their right mind have a function called
getchar return an int?
50
Example Echo
Did you know you can do this with printf? Now you
do.
include ltstdio.hgt int main (void) int ch
printf("Enter some text," "or
Control-d (unix) or Control-z (win)" "
to exit\n") / ECHO INPUT BACK / do
ch getchar() printf("c",
(char)ch) while (ch ! EOF) return
0
We cast the return value.
EOF signals the "end of file". This is Ctrl-D on
unix, or Ctrl-Z on windows.
51
Test Run Shocker!
Enter some text, or Control-d (unix) or Control-z
(win) to edit A A This is a test This is a
test Hey, it echoes entire lines. What
gives? Hey, it echoes entire lines. What gives?
This works, and seems to print out entire lines.
But getchar() was supposed to give us just one
char. What gives?
52
getchar()'s dirty past
In its original Unix form, getchar() did not
return a character until someone hit return.
This is called line-buffering input. ANSI-C
defines getchar() to have this behavior. For
historical reasons, getchar() works this way!
Half-duplex time-sharing!!! If more than one
character was entered, the extra characters are
buffered for subsequent calls to getchar().
H
O
W
?
Enter
Entire line of sample input
The "H" key is return from the first call to
getchar(), and the rest of the line is buffered,
and returned on subsequent calls.
53
Dangerous string inputs
Well, reading a single char can be tedious. How
can we read an entire string? We're about to
cover the most dangerous technique for string
input in C. We're showing you this for
historical reasons, and to help prevent
problems. Use this technique only for the good
of mankind.
Actually if you use this technique you probably
won't be doing mankind any good.
54
gets How NOT to 'get strings'
The gets() method reads a string of characters,
and places them at the address pointed to by its
argument. Let's hope you made enough room for
the input. The prototype for gets() is char
gets(char str) Here, str should be a char
array--a buffer--where you want the input
stored. Normally, you do something like
char buffer256 / some sufficient number /
gets(buffer) / we failed to check
the return value
gets() returns EOF on error / If you don't
provide enough room for the input, the user
overwrites portions of memory, including,
perhaps, your own code.
55
Demonstration
include ltstdio.hgt int main (void) char
buf16 printf("Please enter no more than 15
characters\n") gets(buf) printf("You
entered s", buf) return 0 gcc -o ovflo
ovflo.c (Compiler warns about gets!!!) ovflo Plea
se enter no more than 15 characters Now is the
time for all good men to come to the
aid... Segmentation fault
The gets() method is the source of numerous
security problems. Even the compiler warns
against its use. When the C compiler says its
dangerous, you can believe it.
56
Summary of getchar(), gets()
gets() returns many chars can use backspace to
correct can't return a '\n' security hazard
get your box rooted
getchar() returns a char no backspace
allowed can't return '\n' line-buffers
Don't use gets( )... unless you are an expert !
How can we read a string safely? To do this
right, we need to understand how format
specifiers work. We should start with some
simple printf examples.
57
Questions?
58
Formatting Console I/O
We already know the trick about printing numbers
in C.
int num 12 printf ("Number d \n", num)
The "d" format specification means that a number
argument should be placed in the output. The
"c" format specification means a char should be
used.
59
Format specifications (partial list)
c character d signed decimal
integers i signed decimal integers (historical
version) e scientific notation (e) f
decimal floating point g uses smaller of f
or e o unsigned octal s string
chars u unsigned decimal ints x
unsigned hex (lowercase letters) X unsigned
hex (uppercase) p displays a pointer n
arg is int pointer into which chars printed
so far is placed a percent
sign
For our purposes there are a few useful
formatting specs.
60
Pointers on Pointers (again)
Remember the confusion over pointers? Now you
can print out information that will help you
understand how they work.
include ltstdio.hgt int main (void) int px
int x 0, 1, 2, 3, 4, 5, 6, 7, 8 px
x printf ("Pointer holds address p\n", px)
px printf ("After increment, pointer holds
" "address p\n", px) return 0
What will print out? Let's see ...
61
C\TYC\Klassgttype ptrprnt.c include
ltstdio.hgt int main (void) int px int x
0, 1, 2, 3, 4, 5, 6, 7, 8 px x printf
("Pointer holds address p\n", px) px
printf ("After increment, pointer holds "
"address p\n", px) return
0 C\TYC\Klassgtgcc ptrprnt.c C\TYC\Klassgta P
ointer holds address 0x256f4f4 After increment,
pointer holds address 0x256f4f8 C\TYC\Klassgt
So pointer arithmetic really does work! We've
proved it.
62
Minimum Field Specifications
But printf has many other depths. We can
specify a minimum field value for numbers. For
example, we can use int num printf ("5d",
num) And this guarantees that the 'num' will
take up five spaces. This helps us align columns
very easily. We can also int num printf
("05d", num) to pad the extra spaces with
'zeros'. This is useful for spread sheets.
63
Recall . . .
include ltstdio.hgt / Calculate temp conversion
table / int main (void) int fahr, celc
int min, max, step min -40 max
50 step 5 fahr min while
(fahr lt max) celc 5
(fahr-32)/9 printf ("d\td\n",
fahr, celc) fahr fahr step
Since tabs are not reliable for output
formatting, we can replace this with the minimum
field width specifiers instead.
Let's replace this with
printf ("5d5d\n", fahr, celc)
64
Ahhh.
C\TYC\Klassgtcelc_old -40 -40 -35 -37 -30
-34 -25 -31 -20 -28 -15 -26 -10
-23 -5 -20 0 -17 5 -15 10
-12 15 -9 20 -6 25 -3 30
-1 35 1 40 4 45 7 50 10
C\TYC\Klassgtcelc_new -40 -40 -35 -37 -30
-34 -25 -31 -20 -28 -15 -26 -10 -23
-5 -20 0 -17 5 -15 10 -12 15
-9 20 -6 25 -3 30 -1 35 1
40 4 45 7 50 10
No ragged edges. This is much easier in C than
Java!
Well, almost easier than Java. There's a library
you can use to get printf services in
Java Search for jprintf
65
Other printf magic
Left justification printf ("-d\n",
num) Precision Specification printf
(".4f\n", 123.45678) /
prints out 123.4568, rounded! / Range of
strings printf ("5.10s\n", str) /
prints at least 5, and no more than 10 chars
/ Variable field width printf ("0d\n", i,
num)
field width is a variable
66
C\TYC\Klassgtramp 0 1 02 003 0004 00005 000006 000
0007 00000008 000000009 000000009 00000008 0000007
000006 00005 0004 003 02 1 0
include ltstdio.hgt int main (void) int i
for (i0 i lt 10 i) printf ("0d\n", i,
i) while (i--) printf ("0d\n", i, i)
return 0
Text formatting is easy, and powerful, in C.
67
Back to Reading From Stdin
All of these format specifications are swell, but
what does this have to do with safely reading
input? The procedure scanf() takes in many of
these format specifications, and writes the
keyboard input to a variable. Here's the
prototype int scanf (const char format,
...) Example int x printf ("Enter a
number\n") scanf ("d", x)
This means a variable number of parameters.
Strange, eh?
We give scanf the address of the variable to save
the input into
We want an int from the input
68
scanf
scanf() will stop reading further when a
non-numeric character is found.
int x printf ("Enter a number\n") scanf ("d",
x)
scanf() can also read in strings, which it breaks
on white space, and then null terminates for you
char buff64 printf ("Enter a name\n") scanf
("63s", buff)
Because of the field width specification, we can
prevent buffer overflows. Here, scanf() will not
copy more than 63 chars into our buffer.
69
scanf() and scansets
We can also limit the types of characters that
scanf() will accept XYZ A-Z
2468 Example char
cLetters26 printf ("Enter capital
letters") scanf ("A-Z", cLetter)
Only characters in the brackets will be allowed
as input
Ranges may be used
Scanf() will then copy any input matching the
valid set into the buffer.
70
The power of scanf
include ltstdio.hgt int main (void) char
cAxis2 char buff256 cAxis0 '\0'
buff0 '\0' printf ("Enter axes to rotate,
X, Y and/or Z ") scanf ("1XYZ255s",
cAxis, buff) if (cAxis0) printf ("Valid
input s\n", cAxis) else printf ("Invalid
input.\n") if(strlen(buff)gt0) printf
("You entered extra information s\n", buff)

The user can only enter letters from the set X, Y
or Z. Any additional input is captured into the
extra string buffer--up to 256 characters--and
then printed back at the user in an error
message. We take care to set default values to
null.
71
More features? Wow.
Scanf() can also be conditioned to skip over
characters. For example, if you have four ints,
each separated by a semicolon, you can
use int x, y, w, h scanf ("dddd",
x, y, w, h) The non-white-space characters
in the control string causes scanf() to discard
the characters. Thus, given this
Result
Text line of input
x y w h
103 45 300 75
1034530075
72
Sometimes...
  • ...in the halls of the CoC you will hear people
    say that scanf is broken.
  • And it does appear that there may occasionally be
    strange behaviors seen especially across certain
    complex terminal I/O systems
  • It doesn't help that Unix, Windows and Macs all
    have different ways of terminating lines!

73
fgets -- an even better way
Instead of using gets() or getchar(), we can
instead use fgets(). The signature of fgets()
appears as char fgets (char s, int size,
FILE stream) The fgets() reads in
at most one less character than indicated by the
size parameter. Then, fgets() returns a pointer
to a buffer where it has stored these values. If
a new line is read, it is stored in the buffer.
The nice thing about fgets() is that it
terminates your strings with \0 for you! This
way, you can limit the amount of input you'll
accept from the user (and avoid those
embarrassing buffer overflows).
74
(No Transcript)
75
Upclose...
Size buffer to hold the characters, and the \0'
termination!
include ltstdio.hgt int main (void) char
buff11 char msg buff printf ("This
program accepts only 10 characters at a time.\n"
"Please enter some text ") msg
fgets (msg, 11, stdin) printf ("From what
you've typed, I accepted only\ns\n", msg)
return 0
As noted in the first slides, there's a stream
called "standard in". It's defined as a FILE
(more on this later), but to access it you merely
use 'stdin'
76
char question21 printf ("Any
questions?") scanf ("20s", question)
Note the buffer sizing!
77
File I/O
ANSI-C also supports file I/O. We can get a
handle on a file stream by using a "FILE pointer"
type FILE fp The stdio.h header defines
this type for us. We then just open a file
with fp fopen ("data.txt", "r") / We fail
to check return values here /
The "data.txt" refers to a file on disk, called
"data.txt". The "r" means we want to read from a
text file (and not write, etc.)
78
File I/O
Arsenal of file I/O related calls fopen()
-- open file fclose() -- close file fputc()
-- write char to file fgetc() -- grab
char from file fseek() -- seek to specified
byte in file fprintf() -- print to file
stream fscanf() -- scanf() for a file
stream feof() -- check if end-of-file
reached ferror() -- if error has
occurred remove() -- nuke a file
Some of the more commonly used procedures. Some
of these we've already seen.
79
Opening a File
Java required a series of try/catch exceptions to
handle potential problems in reading a file. C
could care less, but it's a good idea. You have
to check the FILE yourself.
FILE fp fp fopen ("data.txt", "r") if (NULL
fp) printf ("Cannot open file.\n")
exit(1)
The parameters used to indicate the 'mode' in
opening a file include
r open text file for reading w create
text file for writing a append to text
file rb open binary file for reading wb
create binary file for writing ab append to
binary file
r open text file for read/write w
create text file for read/write a append or
create text file for appending rb open
binary file for read/write wb create binary
file for read/write ab append or create
binary file for read/write
80
Reading From a File
At the very least, we can read a character from a
file
include ltstdio.hgt int main (void) FILE fp
char c fp fopen("data.txt", "r") if
(NULL fp) printf ("Unable to open
file\n") exit(1) do
c(char)getc(fp) printf("c", c) while
(c!EOF) fclose(fp) return 0
Be sure to close a file when done reading. This
is done for you on exit(), but NOT when there's
an abort(). So, close your files when done
reading!
81
fscanf and files
We've already seen scanf(), and its flexibility
in reading keyboard input. fscanf() merely
provides the same functions for file I/O. It's
similar to fgets(), shown in the previous
slides. See how a stream, a keyboard, and disk
can all be treated as input streams?
82
Questions?
83
Misc C Points
There are a few loose ends 0) Default
values 1) Access to struct members 2)
Debugging tips (besides "use Java instead") 3)
FYI on KR C coding style
84
Default Values
Recall
Here, we define a structure for a node in a
linked list, and make a head node.
typedef struct node_tag struct
node_tag next int data node
node head
What if we want to allocate a new node?
Is that all?
head.next (node) malloc(sizeof(node))
head.next.next NULL head.next.data 0
You have to NULL terminate your own nodes!
(There is no default value for pointers, like
Java's default null value for references.)
85
In Fact
In fact, don't ever trust any default values in
C you must set them yourself
typedef struct node_tag struct
node_tag next int data node
node head head.next NULL head.data 0
This is important because all of our recursion
techniques will terminate on NULL. If we don't
set the pointers to NULL, we'll never find the
end of the linked list!
86
Access to Structs
Recall typedef struct node_tag int
data struct node_tag next node
void do_something(node curr)
(curr).next (node)malloc(sizeof(node))
((curr).next).next NULL int main
(void) node head do_something
(head)
Normally, to access members of a struct pointer,
we would have to use the operator to
dereference the pointer, and then get the field
value from the struct. This gets ugly.
Note the nesting of dereferencing operators
Is there a better way?
87
The -gt operator
Recall typedef struct node_tag struct
node_tag next int data node void
do_something(node curr) curr-gtnext
(node)malloc(sizeof(node)) curr-gtnext-gtnext
NULL int main (void) node head
do_something (head)
We can instead use the -gt operator, which takes
the place of a "" dereference, and the "." for
getting to field values.
Equivalent (curr).next curr-gtnext
88
Debugging
There are debugging tools that other classes will
emphasize. For now, you can start using "lint"--
a tool that will examine your source code and
report potential problems. You can type "lint"
or "lclint" to invoke this tool. The output
might be a little cryptic, but it will focus your
attention on potential problems.
89
373373 01d Sk00l C H4ck1nG
Trans "elite old school C hacking"
90
(No Transcript)
91
K R Style Function Declaration
Normally, we see function declared and coded like
this
float get_average(int a, int b, int c)
float f (float) (abc)/3 return f
Some of the grizzled old unix hackers still use
"KR" style, the compilers still recognize
FYI You might see this style of coding from
time to time. It works just the same.
float get_average(a, b, c) int a, b, c
float f (float) (abc)/3 return f
92
Questions?
93
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com