lecture26-mar31 - Announcements Lecture 26 Exam 2 on Monday...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Announcements Lecture 26 • Exam 2 on Monday – Sample exam posted, review Friday – Will cover Chapters 11-18, 22*, 26* • Assignment 5 – Due tomorrow, Q&A – If no mode then use mode,-1 to indicate that – Write down your stats (SLOC, hrs, logic errors) • Topics for today – Finish Ch. 18 - advanced declarations/definitions – Random numbers (in Ch. 26.2) #define Vs const #define allows the definition of a preprocessor-time constant that represents a single numerical, character, or string value – Identifiers are not subject to C scope rules const can be used to create run-time constants of any type, including constant arrays, structs and pointers. – Identifiers are subject to C’s scope rules Example const int n = 10; #define N 10 . . . . . . int a[n]; /* illegal */ int a[N]; /* legal */ Declarators: different kinds • In the simplest case, a declarator can be a single identifier: int j; • Declarators may also include a preceding *, indicating “pointer to”: int *p; • A declarator may be followed by square brackets to indicate that it represents an array: int a[10]; • A declarator may be followed by a parameter list to indicate that it represents a function: int f(int i); Declarators: More complex • Declarators can combine *, [ ], and ( ): int *f(int i); /* a function f that returns a ptr to an int */ int (*pf)(int i); /* a function ptr */ int *ap[10]; /* array of ptrs to ints */ int *(*p[ N ]) (void); /* array of ptrs to fcns that return ptrs */ • In a declaration, ( ) and [ ] take precedence over *. Parentheses override normal precedence. • There are some restrictions on declarators: – A function cannot return an array. – An array of functions is impossible (although an array of pointers to functions is legal). Initializers • A declarator that represents a variable may be followed by an initializer: int i = 0, *p = &i, a[5] = {1,2,3,4,5}; • An initializer for an array, structure, or union must contain only constant expressions, never including variables or function calls: #define N 2 int powersOfTwo[5] = {1, N, N*N, N*N*N, N*N*N*N}; • An initializer for a variable with static storage duration is also limited to constant expressions: #define FIRST 1 #define LAST 100 static int i = LAST - FIRST + 1; Initializers (2) • If a variable has automatic storage duration (and is not an array, structure, or union), its initializer can be any expression of the appropriate type—the expression need not be constant: int f(int n) { int last = n - 1;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/30/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

Page1 / 4

lecture26-mar31 - Announcements Lecture 26 Exam 2 on Monday...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online