Class Notes (835,600)
Canada (509,275)
CSC209H1 (41)
Karen Reid (20)
Lecture

feb15c.docx

2 Pages
136 Views
Unlock Document

Department
Computer Science
Course
CSC209H1
Professor
Karen Reid
Semester
Winter

Description
 External and Static Variables o typedef o External variable: declared outside the body of a function  can define new types using typedef, ex. o File scope: visible from point of the declaration to the end of the file typedef unsigned int size_t o Static storage duration: through the duration of the program  ex. o External (global) variables have struct personrec {  File scope char name[20];  To access it in another file, need to do something special int age; };  Static storage duration typedef struct personrec Person;  Unlike variables on the stack that's available only while Person *p = malloc(sizeof(Person)); the function it's declared in is on the stack o Static variables o Header files  static used outside a (function) block means that the variable  When you begin to split up your C program into multiple files, is only visible in the file in which it is declared (file scope) you need header files to store function and type declarations  used to limit the visibility of the variables  Used extern in headers to allow global variables to be  static used in a (function) block means that the variable lives used in other files beyond the duration of the (function) block and is initialized  No memory allocated/declared in the header file only once, ex.  Ex. this implementation only allows 1 linked list  int q is in memory only in the duration of the functionmain.c f, it will always be initialized with the value -1 1. #include "list.h" 2. int main() {  int j only gets initialized once, on the next function 3. add(10); call j will retain its previous value 4. isEmpty(); static int i; // outside a block 5. head = NULL; 6. } void f(void) { int q = -1 list.h list.c static int j; // inside a block 1. struct node { 1. #include "list.h" } 2. int value; 2. List *head = NULL; 3. struct node *next; 3. int isEmpty(int v) { … }  ex. using static as a counter 4. }; 4. void add(int v) { … } 5. typedef struct node List; 5. void remove(int v) { … }  when nextvalue is called again in main, line 4 is 6. extern List *head; skipped and i retains its previous value 7. int isEmpty(int); 8. void add(int); 1. #include 9. void remove(int); 2. 3. int nextvalue() { 4. static int i = 0; 5. i++;  Allow multiple linked lists 6. return i; main.c 7. } 1. #include 2. int main() { 8. 3. List *list1 = NULL; 9. int main() { 4. add(list1, 10); 10. int i; 5. isEmpty(list1); 11. for(i = 10; i > 0; i--) { 6. } 12. printf("%d ", nextvalue()); 13. } list.h list.c 14. return 0; 1. struct node { 1. #include "list.h" 15. } 2. int value; 2. int isEmpty(List *h) { … } 3. struct node *next; 3. void add(List *h,  output: 1 2 3 4 5 6 7 8 9 10 4. }; 4. int v) { … } 5. typedef struct node List; 5. void remove(List *h, o extern used to acess variables or functions outside file scope 6. int isEmpty(List *); 6. int v) { … }  Use extern modifier to tell compiler somewhere else in another void add(List *, int); file there's a variable with the same name, link to that variabled remove(List *, int);  Still have to be in the same object files that's linked  Common mistakes together to create the executable  No memory is allocated for variable w/ extern modifier  Declare a global variable in list.h  memory is being declared in the header file  Ex. stdin, stdout are global variables via extern declaration  Include list.c directly into main.c  will work b/c it in stdio.h copies all the text from list.c into main.c but only if  Ex. global variable in fileb.c that's needed in filea.c there isn't more than 1 included in main.c filea.c fileb.c  Protecting header files 1. extern int i; 1. #include  Compilation errors may result if a header file is included 2. void f(void) { 2. more than once 3. i++; 3.
More Less

Related notes for CSC209H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit