Lecture5-revised version

Lecture5-revised version - C Programming and Debugging Week...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
C Programming and Debugging Week 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
C Programming
Background image of page 2
Types • Pretty Much Like C++ • No classes, but we have structs struct Song { char title[64]; char artist[32]; char composer[32]; short duration; // Playing time in seconds. struct Date published; // Date of publication. };
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Declaring Pointers int *iPtr; //Declare iPtr as a pointer to int iPtr = &iVar //Let iPtr point to the variable iVar int iVar = 77, // Define an int variable int *iPtr = &iVar; // Define a pointer to to it
Background image of page 4
Dereferencing Pointers double x, y, *ptr; // Two double variables and a pointer to double. ptr = &x; // Let ptr point to x. *ptr = 7.8; // Assign the value 7.8 to the variable x. *ptr *= 2.5; // Multiply x by 2.5. y = *ptr + 0.5; // Assign y the result of the addition x + 0.5.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pointer to Pointers char c = ‘A’, *cPtr = &c, **cPtrPtr = &cPtr &cPtr cPtrPtr &c cPtr ‘A’ c
Background image of page 6
Pointers to Functions double (*funcPtr)(double, double); double result; funcPtr = pow; // Let funcPtr point to the function pow( ). // The expression *funcPtr now yields the // function pow( ). result = (*funcPtr)( 1.5, 2.0 ); // Call the function referenced by funcPtr. result = funcPtr( 1.5, 2.0 ); // The same function call.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
typedef Declarations • Easy way to use types with complex names typedef struct Point { double x, y; } Point_t; typedef struct { Point_t top_left; Point_t bottom_right; } Rectangle_t;
Background image of page 8
Dynamic Memory Management • void *malloc(size_t size); – Allocates a block of memory whose size is at least size . • void *realloc(void *ptr, size_t size); – The size of the memory block pointed to by the ptr parameter is changed to the size bytes. • void free(void *ptr) – Frees the block of memory pointed to by ptr.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reading/Writing Characters • Reading/Writing characters – int getchar(); • Returns the next character from the standard input(stdin). • Equivalent to getc with stdin as its argument. – int putchar(int ch); • Writes character to the current position in the standard output(stdout). • Begins copying from the address specified (str) until it reaches the terminating null character('\0'). • The final null-character is not copied to stdout.
Background image of page 10
Reading/Writing Lines • Reading/Writing lines – char *gets(char *str); • Reads characters from stdin and stores them as a string into str until a newline character('\n') or the End-of-File is reached. – int puts(const char * str); • Writes the C string pointed by str to stdout and appends a newline character('\n'). • System Calls – ssize_t read (int fildes , void * buf , size_t nbyte ) – ssize_t write (int fildes , const void * buf , size_t nbyte ) We’ll be covering systems calls next week.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Formatted Input/Output • Formatted Input/Output – int fprintf( FILE * fp , const char * format , . .. ); – int fscanf( FILE * fp , const char * format , . .. ); – FILE *fp • Can be either file pointer or stdin, stdout, or stderr • The format string – int score = 120; char player[ ] = "Mary"; – printf( "%s has %d points.\n", player, score );
Background image of page 12
End-of-File(EOF) • EOF key combination in command line Ctrl + d
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

Lecture5-revised version - C Programming and Debugging Week...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online