Lecture5-revised version

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

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

C Programming and Debugging Week 5

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

View Full Document
C Programming
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. };

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

View Full Document
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
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.

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

View Full Document
Pointer to Pointers char c = ‘A’, *cPtr = &c, **cPtrPtr = &cPtr &cPtr cPtrPtr &c cPtr ‘A’ c
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.

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

View Full Document
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;
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.

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

View Full Document
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.
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.

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

View Full Document
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 );
End-of-File(EOF) • EOF key combination in command line Ctrl + d

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online