1 / 13

C Tutorial Session #3: Pointers Continued: External Variables and Scope

This tutorial session covers external variables and scope in C programming, with a focus on pointers. It also explains the debugging process using the ddd debugger tool.

mfuller
Download Presentation

C Tutorial Session #3: Pointers Continued: External Variables and Scope

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C TutorialSession #3 Pointers continued External Variables and Scope Debugging with ddd

  2. 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

  3. 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;

  4. 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;

  5. 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;

  6. Pass by value int increase(int a){ a++; return a; } int main(){ int foo = 0; int bar = increase(foo); }

  7. Using pointers as arguments int increase (int *a){ (*a)++; } int main () { int foo = 0; int bar = increase (&foo); }

  8. 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

  9. Scope (cont.) file 2: intsp = 0;double val[MAXVAL]; file 1: extern intsp;extern double val[];void push (double f) {…} double pop (void) {…}

  10. 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){ … }

  11. 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.

  12. 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.

  13. Debugging with ddd

More Related