C Programming Chapter04 SEN2201
C Programming Chapter04 SEN2201
Review
Pointers
Void pointers
Function pointers
1
Review:Pointers
1
Review
Pointers
Void pointers
Function pointers
5
Void pointers
5
Function pointers
6
Callbacks
7
Callback (cont.)
8
Callback (cont.)
Consider a linked list with nodes defined as follows:
s t r u c t node {
i n t data ;
s t r u c t node∗ n e x t ;
};
Also consider the function ’apply’ defined as follows:
void a p p l y ( s t r u c t node∗ phead ,
void ( ∗ f p ) ( void ∗ , void ∗ ) ,
void ∗ arg ) / ∗ o n l y f p has t o be named ∗ /
{
s t r u c t node∗ p=phead ;
while ( p ! =NULL )
{
f p ( p , arg ) ; / ∗ can a l s o use ( ∗ f p ) ( p , arg ) ∗ /
p=p−>n e x t ;
}
}
9
Callback (cont.)
Iterating:
s t r u c t node∗ phead ;
/ ∗ p o p u l a t e somewhere ∗ /
void p r i n t ( void ∗ p , void ∗ arg )
{
s t r u c t node∗ np =( s t r u c t node ∗ ) p ;
p r i n t f ( "%d " , np−>data ) ;
}
a p p l y ( phead , p r i n t , NULL ) ;
10
Callback (cont.)
Counting nodes:
11
Array of function pointers
Example:Consider the case where different functions are called
based on a value.
enum TYPE{SQUARE, RECT, CIRCILE ,POLYGON } ;
s t r u c t shape {
f l o a t params [MAX ] ;
enum TYPE t y p e ;
};
void draw ( s t r u c t shape∗ ps )
{
switch ( ps−>t y p e )
{
case SQUARE:
draw_square ( ps ) ; break ;
case RECT:
d r a w _ r e c t ( ps ) ; break ;
...
}
}
12
Array of function pointers
13
Review
Creating Libraries
1
Review: Void pointers
1
Review: Function pointers
2
Review: Function pointers
3
Review
Creating Libraries
5
Symbols and libraries
5
Functions and variables as symbols
i n t main ( void ) {
p u t s ( msg ) ;
return 0;
}
6
Functions and variables as symbols
i n t main ( void ) {
p u t s ( msg ) ;
return 0;
}
6
Functions and variables as symbols
8
Functions and variables as symbols
9
Functions and variables as symbols
10
Static and dynamic linkage
11
Static linkage
12
Static linkage
13
Dynamic linkage
14
Linking external libraries
15
Loading shared libraries
16
Loading shared libraries on demand
17
Loading shared libraries on demand
18
Symbol resolution issues
19
Symbol resolution issues
19
Symbol resolution issues
20
6.087 Lecture 9 – January 22, 2010
Review
Creating Libraries
Data Structures
B-trees
Priority Queues
21
Creating libraries
21
Creating shared libraries
22
Outline
Review
Standard Library
<stdio.h>
<ctype.h>
<stdlib.h>
<assert.h>
<stdarg.h>
<time.h>
1
Review
Standard Library
<stdio.h>
<ctype.h>
<stdlib.h>
<assert.h>
<stdarg.h>
<time.h>
2
Review: Libraries
2
Review
Standard Library
<stdio.h>
<ctype.h>
<stdlib.h>
<assert.h>
<stdarg.h>
<time.h>
5
<stdio.h>: Opening, closing files
FILE∗ fopen(const char∗ filename,const char∗ mode)
• mode can be "r"(read),"w"(write),"a"(append).
• "b" can be appended for binary input/output (unnecessary
in *nx)
• returns NULL on error.
FILE∗ freopen(const char∗ filename,const char∗ mode,FILE∗ stream)
6
<stdio.h>:Temporary files
FILE∗ tmpfile(void)
• creates a temporary file with mode "wb+".
• the file is removed automatically when program
terminates.
char∗ tmpnam(char s[L_tmpnam])
• creates a string that is not the name of an existing file.
• return reference to internal static array if s is NULL.
Populate s otherwise.
• generates a new name every call.
7
<stdio.h>: Raw I/O
8
<stdio.h>: File position
int fseek(FILE∗ stream, long offset,int origin )
• sets file position in the stream. Subsequent read/write
begins at this location
• origin can be SEEK_SET, SEEK_CUR, SEEK_END.
• returns non-zero on error.
long ftell (FILE∗ stream)
• returns the current position within the file. (limitation? long
data type).
• returns -1L on error.
9
<stdio.h>: File errors
10
<ctype.h>: Testing characters
11
<string.h>: Memory functions
void∗ memcpy(void∗ dst,const void∗ src,size_t n)
• copies n bytes from src to location dst
• returns a pointer to dst.
• src and dst cannot overlap.
void∗ memmove(void∗ dst,const void∗ src,size_t n)
• behaves same as memcpy() function.
• src and dst can overlap.
int memcmp(const void∗ cs,const void∗ ct,int n)
• compares first n bytes between cs and ct.
void∗ memset(void∗ dst,int c,int n)
• fills the first n bytes of dst with the value c.
• returns a pointer to dst
12
<stdlib.h>:Utility
int rand()
• returns a pseduo-random numbers between 0 and
RAND_MAX
void srand(unsigned int seed)
• sets the seed for the pseudo-random generator!
13
<stdlib.h>: Exiting
void abort(void)
• causes the program to terminate abnormally.
void exit ( int status)
• causes normal program termination. The value status is
returned to the operating system.
• 0 EXIT_SUCCESS indicates successful termination. Any
other value indicates failure (EXIT_FAILURE)
14
<stdlib.h>:Exiting
15
<stdlib.h>:Searchign and sorting
void ∗ bsearch ( const void ∗ key , const void ∗ base ,
s i z e _ t n , s i z e _ t size ,
i n t ( ∗ cmp ) ( const void ∗ keyval , const void ∗ datum ) ) ;
16
<assert.h>:Diagnostics
17
<stdarg.h>:Variable argument lists
18
<stdarg.h>:Variable argument list
19
<stdarg.h>:Variable argument list(cont.)
i n t sum ( i n t num , . . . )
{
v a _ l i s t ap ; i n t t o t a l =0;
v a _ s t a r t ( ap , num ) ;
while ( num>0)
{
t o t a l +=va_arg ( ap , i n t ) ;
num−−;
}
va_end ( ap ) ;
return t o t a l ;
}
i n t suma=sum ( 4 , 1 , 2 , 3 , 4 ) ; / ∗ c a l l e d w i t h f i v e args ∗ /
i n t sumb=sum ( 2 , 1 , 2 ) ; / ∗ c a l l e d w i t h t h r e e args ∗ /
20
<time.h>
21
<time.h>
clock_t clock ()
• returns processor time used since beginning of program.
• divide by CLOCKS_PER_SEC to get time in seconds.
time_t time(time_t ∗ tp)
• returns current time (seconds since Jan 1 1970).
• if tp is not NULL, also populates tp.
double difftime(time_t t1 ,time_t t2)
• returns difference in seconds.
time_t mktime(struct tm∗ tp)
• converts the structure to a time_t object.
• returns -1 if conversion is not possible.
22
<time.h>
23
<time.h>
size_t strftime (char∗ s,size_t smax,const char∗ fmt,const struct tm∗ tp)
24