130 likes | 323 Views
C Tutorial Session #3. Pointers continued External Variables and Scope Debugging with ddd. Review: pointers. Pointer variable presents a memory address. The difference between pointer variables and regular variables. int A. int *B. Stores the value of A. Stores a memory address
E N D
C TutorialSession #3 Pointers continued External Variables and Scope Debugging with ddd
Review: pointers • Pointer variable presents a memory address. • The difference between pointer variables and regular variables. int A int *B Stores the value of A Stores a memory address of an integer memory
Example 1 foo int foo = 100; int *bar; 100 0x1000 bar uninitialized 0x1020 Question: what is 1. foo; 2. &foo; 3. bar; 4. *bar; 5. &bar;
Example 2 foo int foo = 100; int *bar; bar = &foo; 100 0x1000 bar 0x1000 0x1020 Question: what is 1. foo; 2. &foo; 3. bar; 4. *bar; 5. &bar;
Example 3 foo int foo = 100; int *bar; bar = &foo; *bar = 200; 100 200 0x1000 bar 0x1000 0x1020 Question: what is 1. foo; 2. &foo; 3. bar; 4. *bar; 5. &bar;
Pass by value int increase(int a){ a++; return a; } int main(){ int foo = 0; int bar = increase(foo); }
Using pointers as arguments int increase (int *a){ (*a)++; } int main () { int foo = 0; int bar = increase (&foo); }
Variables and Scope • Variables declared within a function • private, local, automatic • can only be accessed within that function • External (Global) variables • Declared ONCE at the top of ONE source file • Other source files may access/declare that same variable using the extern declaration
Scope (cont.) file 2: intsp = 0;double val[MAXVAL]; file 1: extern intsp;extern double val[];void push (double f) {…} double pop (void) {…}
Definitions and Declarations among shared files calc.h #define NUMBER '0' void push (double); double pop (void); intgetop (char []);intgetch (void);void ungetch (int); main.c getop.c stack.c #include <stdio.h>#include <stdlib.h>#include "calc.h“#define MAXOP 100main () { … } #include <stdio.h>#include <ctype.h>#include "calc.h"getop () { … } #include <stdio.h>#include "calc.h"#define MAXVAL 100intsp = 0;double val[MAXVAL];void push (double) { … }double pop (void){ … } getch.h #include <stdio.h>#define BUFSIZE 100char buf[BUFSIZE];intbufp = 0;intgetch (void) { … }void ungetch (int){ … }
Static Global Variables • Declaring a variable (or functions) as static limits the scope to the rest of the source file being compiled. static char buf [BUFSIZE] /* buffer for ungetch */static intbufp = 0; /* next free position in buf */ intgetch (void) { … }void ungetch (int c) { … } • The static declaration means that both functions within this source file can access these variables, but others can’t.
Static Local Variables In a compiled program there are 2 main areas for variable storage: • Stack – used for called functions and local variables. Goes away when a function returns. • Heap – used for global allocation and external variables. Declaring a local variable as static, allocates it on the heap. This is useful for large arrays and allocations.