Download presentation
Presentation is loading. Please wait.
1
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
2
Functions /* Increment; takes an integer argument and * returns the argument plus one. */ int incr (int i) { int j; j = i + 1; return j; } main () { int k, m = 4; k = incr(m); printf ("k = %d, m = %d\n", k, m); } output: k = 5, m = 4
3
More about Functions might have no return type, and no return statement: void printhi () { printf ("hi\n"); } parameters are copied and can be modified int incr (int i) { i++; return i; } default (unspecified) return type is int
4
Variables within Functions But this does not work: void no_incr (int i) { i++; } void main () { int x = 5; no_incr(x); printf ("%d\n", x); } beware that modifications are on internal copies of the parameters. note: void main() since main does not return a value
5
C Preprocessor Directives Operations before compilation phase: include "header" files that contain pre-supplied functions: #include this line will be substituted by the contents of the file define symbolic constants: #define MY_LARGE_NUMBER 12345 #define MY_NIFTY_STRING "abc"
6
Make your own booleans C has boolean operations on int: == != && || ! However, C has no boolean type. Make it! #define boolean_t int; #define TRUE 1 #define FALSE 0 Example: boolean_t xIsFive, zAsBig, result; xIsFive = (x == 5); zAsBig = (z >= x); if (xIsFive || zAsBig) { result = TRUE; }
7
Naming your own type A better way: use typedef typedef int boolean_t; Syntax: typedef existing-type new-type; Comments: typedef does not create a new type only creates a new label for an existing type makes your code easier to read
8
typedef in action Example: typedef char * string_t; typedef int boolean_t; string_t msg = "No more room"; boolean_t full_class; if (num_students >= MAX_SIZE) full_class= TRUE;
9
i == *pi Pointers C relies heavily on pointers: int i; int *pi; pi = &i; *pi = 3; 3 pi
10
Pointers and Arrays An array name by itself is a pointer to the first element: int a[100]; *a = 5; /* same as a[0] = 5 */ *(a + 3) = 10; /* same as a[3] = 10; */ /* Note: *a + 3 means something else */ Strings are arrays of char: char s[3]; strcpy (s, "hi"); /* same as following line */ s[0] = 'h'; s[1] = 'i'; s[2] = '\0';
11
4 j i Call by reference void incr (int *i); /* * increment i by 1 */ { *i = *i + 1; } main() { int j; j = 4; incr (&j); printf ("j = %d\n", j); } &j and i are pointers to the same integer: incr can change the integer (side effect), but not the pointer
12
Dynamic Memory Allocation Pointer errors can be nasty void main () { int *p; *p = 5; } Reserve space for the "pointed to" object: #include void main () { int *p; p = (int *)malloc (sizeof (int)); *p = 5; printf ("value at p = %d\n", *p); free (p);/* must do this yourself! */ }
13
Structures Use struct to create or define a new type: struct str_data { char *string; int length; }; struct str_data s1, s2; Syntax struct structure_name { type1 member1; type2 member2; … }; A member of a structure is referred to by: structure_name.member
14
Structures, cont. Example: struct str_data { char *string; int length; }; struct str_data s1, s2; s1.string = (char *) malloc(80); strcpy (s1.string, "How now brown cow."); s1.length = strlen(s1.string); s2 = s1; /* can copy structures, pass to fcns */
15
Arrays of Structures Example: #define MAX_STUDENT 100 typedef struct { /* define a new type */ char name[80]; int name_len; int student_number; } student_t; /* create list of elements of type student_t */ student_t class[MAX_STUDENT]; for (i = 0; i < MAX_STUDENT; i++) { gets(class[i].name); class[i].name_len = strlen(class[i].name); }
16
Pointers to Structures These are all equivalent: list[0].length =... (*list).length =... list->length =... Rules: p->y is shorthand for (*p).y means "follow the pointer p into structure member y more efficient to pass pointers to structs rather than the structs themselves
17
Self-Referential Structures Example: Linked List This does not work: typedef struct { int data; node_t *next; } node_t; Rules: a structure cannot refer to its own name before it is defined but it can contain a pointer to itself how?
18
Naming your structure Give the structure a name: typedef struct element { int data; struct element *next; } node_t; Rules: next is a pointer to a structure of type node_t we can use this to create a pointer to the head of the list: node_t *head = (node_t *) malloc (sizeof (node_t)); How do we define the end of the list?
19
Traversing a Linked List node_t *p; p = head; while (p != NULL) { printf ("data = %d\n", p->data); p = p->next; } data next data next head: p: p->next:
20
Command Line Arguments Command line: c:\app1.exe argument1 argument2 argument3... Necessary code: int main(int argc, char *argv[]) { if (argc !=3) { printf(“Error!\n”); exit(1); } … } argc – Number of command line arguments, including the app name. argv – An array containing the entire command line, separated into arguments.
21
Using Files Defining a pointer to a file: FILE *file_pointer; Opening a file: Syntax : FILE * fopen ( const char * filename, const char * mode ); Mode : “r” - read “w” - write “a” - append Example: file_pointer = fopen(“file.ext”, “r”);
22
Using Files - cont Reading from files : Syntax: int fscanf ( FILE * stream, const char * format,... ); Example: fscanf(file_pointer, “%s”, str); Note: Other file reading functions : fread, fgets, fgetc
23
Using Files - cont Writing to files : Syntax: int fprintf ( FILE * stream, const char * format,... ); Example: fprintf(file_pointer, “%s”, “Hello File!”); Note: Other file writing functions : fwrite, fputs, fputc
24
Using Files - cont Closing files : Syntax: int fclose ( FILE * stream ); Example: fclose(file_pointer); Note: Make sure you always close files before exiting your program.
25
Using Files - cont Looping to the end of files : Syntax: int feof ( FILE * stream ); Example: while (!feof(file_pointer)) { c = fgetc (file_pointer); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.