Download presentation
Presentation is loading. Please wait.
1
1 Review of Class on Oct. 28
2
2 Outline Pointer Pointers to void Call-by-Reference Basic Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
3
3 Pointer Pointers: Pointer variables can be declared in programs and then used to take addresses as values Declaration odouble *pb, x; Assignment opb = &x; Dereferencing oAccess the value of the variable to which the pointer points by dereference operator *: *pb = 3; What is the value of x? *pointer pointer = address; DataType *pointer1, *pointer2;
4
4 Pointer Pointers: What is the legal range of values of any pointer? a set of positive integers that are interpreted as machine addresses on a particular C system, and the special address NULL, 0 ostdio.h: #define NULL 0 oNULL can be used to represent false while(NULL) printf(“loop\n”);
5
5 Pointer #include int main(void){ int a =1,b =2; int *pa, *pb; pa = &a; pb = &b; printf("value of b: %d \n", *pb); *pb=3; printf("Input an integer: "); scanf("%d", pa); } Declaration of a pointer: DataType *pointer; Assign address to a pointer: pointer = address; Access the value of the variable to which the pointer points: *pointer
6
6 Outline Pointer Pointers to void Call-by-Reference Basic Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
7
7 Call-by-Reference Call-by-value Function Invocation fun_name(exp1, exp2); All arguments are passed call-by-value Each argument is evaluated, and its value is used locally in place of the corresponding formal parameter. If a variable is passed to a function, the stored value of that variable in the calling environment is not changed.
8
8 Call-by-Reference #include void swap(int, int); int main(void){ int a=3, b=7; swap(a,b); printf("main: %d %d\n", a, b); return 0; } void swap(int sa, int sb){ int tmp; tmp = sa; sa = sb; sb = tmp; printf("swap: %d %d\n", sa, sb); } % a.out swap: 7 3 main: 3 7 Memory a b 7 3 swap: sa swap: sb swap: tmp 7 3 3 7 3
9
9 Call-by-Reference #include void swap(int*, int* ); int main(void){ int a=3, b=7; printf("Addr %u, %u\n", &a,&b); swap(&a,&b); printf("main: %d %d\n", a, b); return 0; } void swap(int *pa, int *pb){ int tmp; tmp = *pa; *pa = *pb; *pb = tmp; printf("swap: %d %d\n", *pa, *pb); } % a.out Addr 4290705268, 4290705264 swap: 7 3 main: 7 3 Memory a b 7 3 swap: pa swap: pb swap: tmp 3 4290705268 4290705264 4290705268 4290705264 7 3
10
10 Call-by-Reference #include void swap(int*, int* ); int main(void){ int a=3, b=7; printf("Addr %u, %u\n", &a,&b); swap(&a,&b); printf("main: %d %d\n", a, b); return 0; } void swap(int *a, int *b){ int tmp; tmp = *a; *a = *b; *b = tmp; printf("swap: %d %d\n", *a, *b); } How to modify the values of the variables referred to in the argument list? Declaring a function parameter to be a pointer Using the pointer in the function body Passing an address as an argument when the function is called
11
11 End of Review
12
12 Class on Nov 2
13
13 Outline Pointer Pointers to void Call-by-Reference Basic Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
14
14 Pointers to void In ANSI C, one pointer can be assigned to another only when they both have the same type, or when one of them is of type pointer to void void * can be considered as a generic pointer type NULL can be assigned to any pointer. Example: int *p; double *q; void *v; p = 0; p = NULL; p = (int *) 1; p = v = (int *) q; p = q; p =1; /* legal */ /* illegal */
15
15 Outline Pointer Pointers to void Call-by-Reference Basic Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
16
16 Given a variable, Date type set aside an appropriate amount of memory instruct the machine to perform specified operations correctly. Where can we use this variable in the code? The scope of the variable During the program execution, when is the memory allocated to the variable and when is the memory released? The lifetime of the variable Storage Class
17
17 Outline Pointer Pointers to void Call-by-Reference Basic Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
18
18 Scope Rules include int f(int n); int main(void){ int i; for (i=1; i<=8; i++) printf("sum of 1 to %d = %d\n",i, f(i)); } /* calculate 1+2+3+…..+n */ int f(int n){ int i, sum=0; for (i=0; i<=n; i++) sum += i; return sum; } Example print out the results of the following expressions: 1 1+2 1+2+3 1+2+3+4 1+2+3+4+5 1+2+3+4+5+6 1+2+3+4+5+6+7 1+2+3+4+5+6+7+8 Can we have two variables with the same name i? the scope of an identifier
19
19 Scope Rules What is the scope of an identifier? The part of the code in which the identifier is accessible. include int f(int n); int main(void){ int i; for (i=1; i<=8; i++) printf("sum of 1 to %d = %d\n",i, f(i)); } int f(int n){ int i, sum=0; for (i=0; i<=n; i++) sum += i; return sum; }
20
20 Scope Rules How to decide the scope of a given identifier? This concept depends on the notion of a block, which is a compound statement with declarations.
21
21 Scope Rules include int f(int n); int main(void) { int i; for (i=1; i<=8; i++) printf("sum of 1 to %d = %d\n",i, f(i)); } int f(int n) { int i, sum=0; for (i=0; i<=n; i++) sum += i; return sum; } The body of function definition can be considered as a block.
22
22 Scope Rules How to decide the scope of a given identifier? This concept depends on the notion of a block, which is a compound statement with declarations. Identifiers are accessible only within the block in which they are declared. They are unknown outside the boundaries of that block.
23
23 Scope Rules include int f(int n); int main(void) { int i; for (i=1; i<=8; i++) printf("sum of 1 to %d = %d\n",i, f(i)); } int f(int n) { int i, sum=0; for (i=0; i<=n; i++) sum += i; return sum; } The i declared in function main is unknown only one variable with name i is accessible The i declared in function f is unknown only one variable with name i is accessible Variables are unknown outside the block in which they are declared.
24
24 Scope Rules #include int main(void){ { int b =1; printf("%d\n", b); } printf("%d\n", b); return 0; } Identifiers are accessible only within the block in which they are declared. % gcc scope3.c scope3.c: In function `main': scope3.c:7: error: `b' undeclared (first use in this function) scope3.c:7: error: (Each undeclared identifier is reported only once scope3.c:7: error: for each function it appears in.)
25
25 Scope Rules #include int main(void){ int a = 2; int *p = &a; printf("%d\n", a); { int a= 7; printf("%d\n", a); printf("%d\n", *p); } printf("%d\n", ++a); return 0; } % a.out 2 7 2 3 the inner block a hides the outer block a What if same identifier used in different declarations?
26
26 Scope Rules include int f(int n); int main(void) { int i; float i; for (i=1; i<=8; i++) printf("sum of 1 to %d = %d\n",i, f(i)); } int f(int n) { int i, sum=0; for (i=0; i<=n; i++) sum += i; return sum; } We cannot declare two variables with the same name in the same block. % gcc scope0.c scope0.c: In function `main': scope0.c:5: error: conflicting types for `i' scope0.c:4: error: previous declaration of `i' %
27
27 Scope Rules Summary The scope of an identifier The part of the code in which the identifier is accessible. This concept depends on the notion of a block, which is a compound statement with declarations. The basic rule of scoping Identifiers are accessible only within the block in which they are declared. They are unknown outside the boundaries of that block. It is not allowed to declare more than one variable with the same name within a block.
28
28 Outline Pointer Pointers to void Call-by-Reference Basic Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
29
29 Given a variable, Date type Set aside an appropriate amount of memory Performance correct instructions for operations on the variable Where can we use this variable in the code? The scope of the variable During the program execution, when is the memory allocated to the variable and when is the memory released? The lifetime of the variable Storage Class
30
30 Storage Classes What is an identifier’s storage class? An important attribute of an identifier, scope of the code where it is accessible time span that it exists in memory during program execution Storage Classes: automatic, external, register, static, and static external oDeclaration oscope oLife span
31
31 Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile Outline Automatic External Register Static Static external
32
32 Storage Classes — auto How to declare an automatic variable? Block: A compound statement with declarations By default, variables declared within blocks are of storage class automatic. The keyword auto can be used to explicitly specify the storage class
33
33 include int f(int n); int main(void) { int i; for (i=1; i<=8; i++) printf("sum of 1 to %d = %d\n",i, f(i)); } int f(int n) { int i, sum=0; for (i=0; i<=n; i++) sum += i; return sum; } Storage Classes — auto
34
34 Storage Classes — auto Scope: The block in which it is declared Time span: When a block is entered, the system allocates memory for the automatic variables. Within that block, these variables are defined and are considered local to the block When the block is exited, the system releases the memory that was set aside for the automatic variables. The values of these variables are lost If the block is reentered, the system once again allocates memory, but previous values are lost.
35
35 Storage Classes DeclarationScopeLife span Autowithin a block block in which it is declared Memory is allocated when the block is entered and released when the block is exited ExternOutside a function All the functions declared after it The whole execution (Storage is permanently assigned) Register(register) within a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited Static(static) within a block Block in which it is declared The whole execution (Storage is permanently assigned) Static extern (static) Outside a function The remainder of the file in which it is declared The whole execution (Storage is permanently assigned)
36
36 Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile Outline Auto External Register Static Static external
37
37 Storage Classes — extern extern variable a variable’s storage class is extern if it is declared outside a function Keyword extern int a=1, b=2, c=3; is equivalent to extern int a=1, b=2, c=3; #include int a=1, b=2, c=3; int f(void); int main(void){ a = b = c = 4; printf("%3d\n", f()); return 0; } int f(void){ return (a+b+c); }
38
38 Storage Classes — extern Scope: Global to all functions declared after it. life span of extern variable Storage is permanently assigned to it Upon exit from the block or function, the external variables retain their values. Why external variables? They can be used to transmit values across functions
39
39 Storage Classes — extern #include int a=1, b=2, c=3; int f(void); int main(void){ printf("%3d\n", f()); printf("%3d%3d%3d\n", a, b, c); return 0; } int f(void){ a = b = c = 4; return (a+b+c); } % a.out 12 4 4 4 The external variables retain their values across block and function exit.
40
40 Storage Classes — extern #include int a=1, b=2, c=3; int f(void); int main(void){ printf("%3d\n", f()); printf("%3d%3d%3d\n", a, b, c); return 0; } int f(void){ int b, c; a = b = c = 4; return (a+b+c); } % a.out 12 4 2 3 They may be hidden if the identifier is redefined
41
41 Storage Classes — extern external variables in large program The ability to compile files separately is important when writing large program. Question: When a file is compiled, how can the compiler gets the information of the external variables declared in other file?
42
42 Storage Classes — extern #include int a=1, b=2, c=3; int f(void); int main(void){ printf("%3d\n", f()); printf("%3d%3d%3d\n", a, b, c); return 0; } file1.c int f(void){ int b, c; a = b = c = 4; return (a+b+c); } file2.c When file2.c is compiled separately, how can the compiler gets the information of external variable a?
43
43 Storage Classes — extern #include int a=1, b=2, c=3; int f(void); int main(void){ printf("%3d\n", f()); printf("%3d%3d%3d\n", a, b, c); return 0; } file1.c int f(void){ int b, c; a = b = c = 4; return (a+b+c); } file2.c % gcc file1.c file2.c file2.c: In function `f': file2.c:3: error: `a' undeclared (first use in this function) file2.c:3: error: (Each undeclared identifier is reported only once file2.c:3: error: for each function it appears in.)
44
44 Storage Classes — extern #include int a=1, b=2, c=3; int f(void); int main(void){ printf("%3d\n", f()); printf("%3d%3d%3d\n", a, b, c); return 0; } file1.c int f(void){ extern int a; int b, c; a = b = c = 4; return (a+b+c); } file2.c The use of extern in file2.c tells the compiler that the variable a is defined elsewhere
45
45 Storage Classes DeclarationScopeLife span Autowithin a block block in which it is declared Memory is allocated when the block is entered and released when the block is exited ExternOutside a function All the functions declared after it The whole execution (Storage is permanently assigned) Register(register) within a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited Static(static) within a block Block in which it is declared The whole execution (Storage is permanently assigned) Static extern (static) Outside a function The remainder of the file in which it is declared The whole execution (Storage is permanently assigned)
46
46 Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile Outline Auto Extern Register Static Static external
47
47 Storage Classes — register How to declare register variables? register int i; Scope and life Span, Same as automatic variables
48
48 Storage Classes — register Features of register variables Tells the compiler that the variables should be stored in high-speed memory registers, if possible. Because resource limitations sometimes make this impossible, this storage class defaults to automatic whenever the compiler cannot allocate a register. Purpose Improve execution speed
49
49 Storage Classes DeclarationScopeLife span Autowithin a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited ExternOutside a function All the functions declared after it The whole execution (Storage is permanently assigned) Register(register) within a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited Static(static) within a block Block in which it is declared The whole execution (Storage is permanently assigned) Static extern (static) Outside a function The remainder of the file in which it is declared The whole execution (Storage is permanently assigned)
50
50 Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile Outline Auto Extern Register Static Static external
51
51 Storage Classes — static static declarations static int count; Scope: private to the block in which they are declared Life Span: the whole time the program is executing Allow a local variable to retain its previous values when the block is reentered. Contrast to automatic variables, which lose their value upon block exit.
52
52 #include int is_prime(int n){ int k, limit; if (n==2) return 1; if (n%2==0) return 0; limit = n/2; for (k=3; k<=limit; k+=2) if (n%k==0) return 0; return 1; } int increment_count(void){ static int count = 0; return (++count); } int main(void){ int i=0; printf("Primes less than 30:\n"); for(i=2; i<=30; i++) if (is_prime(i)) printf("prime %3d: %4d\n",increment_count(),i); } % a.out Primes less than 30: prime 1: 2 prime 2: 3 prime 3: 5 prime 4: 7 prime 5: 11 prime 6: 13 prime 7: 17 prime 8: 19 prime 9: 23 prime 10: 29 a static variable retains its previous values when the block is reentered.
53
53 Storage Classes DeclarationScopeLife span Autowithin a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited ExternOutside a function All the functions declared after it The whole execution (Storage is permanently assigned) Register(register) within a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited Static(static) within a block Block in which it is declared The whole execution (Storage is permanently assigned) Static extern (static) Outside a function The remainder of the file in which it is declared The whole execution (Storage is permanently assigned)
54
54 Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile Outline Auto Extern Register Static Static external
55
55 #include “count.h” static int count = 0; int increment_count(void){ return (++count); } int get_count(void){ return count; } #include #include "count.h" int is_prime(int n){ …… } int main(void){ int i=0; for(i=2; get_count()<3000; i++) { if (is_prime(i)) increment_count(); if( i%1000 ==0 || get_count()==3000) printf("%8d %8d\n", i, get_count()); } se.c count.c Variable count in file count.c accessible by increment_count() get_count() Unknown outside of file count.c --- private to count.c int get_count(void); int increment_count(void); count.h gcc se.c count.c %a.out 1000 168 2000 303 …… 26000 2860 27000 2961 27449 3000 Use functions provided in count.c to keep track of the number of primes less than or equal to i.
56
56 Storage Classes — static external How to declare static external variables? keyword: static, declared outside a function Example static int count = 0; int increment_count(void){ return (++count); } int get_count(void){ return count; }
57
57 Storage Classes — static external Scope the remainder of the source file in which they are declared Life Span Storage is permanently assigned. values are retained #include “count.h” static int count = 0; int increment_count(void){ return (++count); } int get_count(void){ return count; }
58
58 Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile Auto External Register Static Static external Outline
59
59 Storage Classes DeclarationScopeLife span Autowithin a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited ExternOutside a function All the functions declared after it The whole execution (Storage is permanently assigned) Register(register) within a block Block in which it is declared Memory is allocated when the block is entered and released when the block is exited Static(static) within a block Block in which it is declared The whole execution (Storage is permanently assigned) Static extern (static) Outside a function The remainder of the file in which it is declared The whole execution (Storage is permanently assigned)
60
60 Outline Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
61
61 Default Initialization Default Initialization Both external variables and static variables that are not explicitly initialized by the programmer are initialized to zero by the system.
62
62 Default Initialization Default Initialization Automatic and register variables usually are not initialized by the system Start with garbage values. Although some C systems do initialize automatic variables to zero, this feature should not be relied on.
63
63 Default Initialization #include int i; void f(){ static int a; printf("%d\n", a); } int main(void){ int b; printf("%d\n", b); printf("%d\n", i); f(); } % a.out -4261900 0
64
64 Outline Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
65
65 Outline Pointer Pointers to void Call-by-Reference Scope Rules Storage Classes Default Initialization Function Declarations and Definitions The Type Qualifiers const and volatile
66
66 End of Chapter 8 Read 8.1 – 8.14
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.