Topics 22440 Introduction to Microprocessors C-Language Review (I) Important: : You will not learn how to code in C in this one lecture! You ll still need some sort of C reference. C Syntax Important Tidits in C Pointers Passing Arguments to Functions Saeid Nooshaadi saeid@unsw.edu.au 1 22440 Introduction to Microprocessors 2 Compilation C compilers take C and convert it into an architecture specific machine code (string of 1s and 0s). Unlike Java which converts to architecture independent code. Unlike Mathematica and Matla environments which interpret the code. Advantages of C-style C compilation: Great run-time performance: generally much faster than Java for comparale code (ecause it optimizes for a given architecture) OK compilation time: enhancements in compilation procedure (Makefiles( Makefiles) ) allow only modified files to e recompiled 22440 Introduction to Microprocessors 3 22440 Introduction to Microprocessors 4
Disadvantages of C-style C compilation: All compiled files (including the executale) are architecture specific, depending on oth the CPU type and the operating system. Executale must e reuilt on each new system. Called porting your code to a new architecture. The change compile run [repeat] iteration cycle is slow 22440 Introduction to Microprocessors 5 main is called y OS C Syntax #include <stdio.h> int main (void) { unsigned int exp = 1; int k; /* Compute 2 to the 31st.*/ for (k=0; k<31; k++) { exp = exp * 2; return 0; Replaces current line y contents of specified file; < > means look in system area, " " user area. Declare efore use; each sequence of variale declarations must follow a left race. /* */ indicate commnets. gcc accepts // comments ut they aren t legal C. 22440 Introduction to Microprocessors 6 C Syntax: General C Syntax: Declarations Very similar to Java, ut with a few minor ut important differences Header files (.h(.h)) contain function declarations, just like in C++..c files contain the actual code. main () is called y OS main can have arguments (more on this later): int main (int( argc,, char *argv* argv[]) In no argument correct form is: int main (void) Comment your code: only /* */ works // doesn t t work in C gcc accepts // 22440 Introduction to Microprocessors 7 All declarations must go at the eginning of a C lock, efore assigning any values. Examples of incorrect declarations: int c = 0; c = c + 1; d = 23; /* error */ char d; for (int( i = 0; i < ; i++) 22440 Introduction to Microprocessors 8
C Syntax: Structs C uses struct instead of classes, ut they re very similar. Sample declaration: struct alpha { int a; char ; ; To create an instance of this struct: struct alpha inst1; Read up on more struct specifics in a C reference. 22440 Introduction to Microprocessors 9 True or False What evaluates to FALSE in C 0 (integer) NULL (pointer:( more on this later) What evaluates to TRUE in C everything else No such thing as a Boolean type in C. 22440 Introduction to Microprocessors Address vs. Value Consider memory to e a single huge array: Each cell of the array has an address associated with it. Each cell also stores some value. Don t t confuse the address referring to a memory location with the value stored in that location. Addresses 1 word = 4 Bytes = 32 its 88 24 65 32 90 a c d Variales 1 2 3 4 5 23 42 Variale Address Variale Value 22440 Introduction to Microprocessors 11 22440 Introduction to Microprocessors 12
Addresses Variale Address Variale Value 1 word = 4 Bytes = 32 its 65 88 32 4 24 a 90 c d ptr variales 22440 Introduction to Microprocessors 13 Pointers in C An address refers to a particular memory location. In other words, it points to a memory location. Pointer: : High Level Language (in this case C) way of representing a memory address. Location (address) name 1 2 3 4 5 23 42 4 y p x More specifically, a C variale can contain a pointer to something else. It actually stores the memory address that something else is stored at. 22440 Introduction to Microprocessors 14 Why use pointers If we want to pass a huge struct or array, it s s easier to pass a pointer than the whole thing. In general, pointers allow cleaner, more compact code. So what are the drawacks Pointers are proaly the single largest source of ugs in software, so e careful anytime you deal with them. 96 24 a 65 32 90 c d 45 a 55 11 c 88 d Struc #1 Struc #2 How How to create a pointer: & operator: get address of a variale int *p, int x; p x x = 3; p x 3 p = &x; p x 3 How How get a value pointed to * dereference operator : : get value pointed to Note the * gets used 2 different ways in this example. In the declaration to indicate that p is going to e a pointer, and in the printf to get the value pointed to y p. Struc #n printf( p points to %d\n,*p); 22440 Introduction to Microprocessors 15 22440 Introduction to Microprocessors 16
How How to change a variale pointed to Use dereference * operator on left of = p x 3 *p = 5; p x 5 To declare a pointer, just precede the variale name with a * Examples: int *a; Pointers int ; int *ptr, var,, var2; Warning: : In the third example aove, the variale ptr is a pointer to an integer, while var and var2 are actual integer variales. The asterisk only applies to one variale. 88 ptr 4 1 word = 4 Bytes a var var2 22440 Introduction to Microprocessors 17 22440 Introduction to Microprocessors 18 Mixing int and char pointers Examples: int *a; char *; int *ptr, var; char *chptr* chptr, ch; Pointers int Size: 1 word = 4 Bytes = 32 its char Size: 1 Byte = 8 its a 88 ptr 4 1 word = 4 Bytes 0,1,2, 3 var chptr ch Notice that a pointer can only point to one type (int( int, char,, a struct,, etc.). An int* cannot point to a character, or vice versa. Why not Safety: Pointers are known to cause prolems to careless programmers, so limit what a pointer can do. void * is a type that can point to anything (generic pointer) Use sparingly to help avoid program ugs! 22440 Introduction to Microprocessors 19 22440 Introduction to Microprocessors 20
Arguments to Functions Arguments can e: passed y value: Make a copy of the original argument (doesn t t really affect types such as integers). int p : addone (p) passed y reference: Pass a pointer, so the called function makes modifications to the original struct. int *p : addone (p) Passing y reference can e dangerous, so e careful. Pointers and Passing Arguments Java Java and C pass an y value procedure/function gets a copy of the parameter, so changing the copy cannot change the original void addone (int x) { x = x + 1; int y = 3; addone(y); y is still = 3 22440 Introduction to Microprocessors 21 22440 Introduction to Microprocessors 22 How How to get a function to change a value void addone (int *p) { *p = *p + 1; int y = 3; addone(&y); y is now = 4 22440 Introduction to Microprocessors 23 Arguments to Functions: Example int a =1, =1 *alpha = &a, *eta = &; void point_less (int( a, int ) { a = 0; = 0; void point_full (int( *a, int *) { *a = 0; * = 0; 88 96 point_less(a,); /* After calling point_less, and are unchanged */ point_full(alpha,eta); /* After calling point_full,a and are changed */ a alpha eta 22440 Introduction to Microprocessors 24
Peer Instruction Question Peer Instruction Answer void main(); { int *p, x=5, y; // init y = *(p = &x) + ; int z; flip-sign(p); printf("x=%d,y=%d,p=%d =%d,y=%d,p=%d\n",x,y,p); flip-sign(int *n){*n = -(*n) How many errors #Errors 1 2 3 4 5 6 7 8 9 (1)0 22440 Introduction to Microprocessors 25 void main(); { int *p, x=5, y; // init y = *(p = &x) + ; int z; flip-sign(p); printf("x=%d,y=%d,p=%d =%d,y=%d,p=%d\n",x,y, n",x,y,*p); flip-sign(int *n){*n = -(*n); How many errors I get 7. #Errors 1 2 3 4 5 6 7 8 9 (1)0 22440 Introduction to Microprocessors 26 References: Nick Parlante: Stanford CS Education Lirary (http://cslirary.stanford.edu cslirary.stanford.edu/); A Collection of very useful material including: Essential C: (http://cslirary.stanford.edu/1/( http://cslirary.stanford.edu/1/) ) A relatively quick, 45 page discussion of most of the practical aspects of programming in C. Binky Pointer Video: : (http://cslirary.stanford.edu//4( http://cslirary.stanford.edu//4) ) Silly ut memorale 3 minute animated video demonstrating the asic structure, ure, techniques, and pitfalls of using pointers. Pointer Basics: : (http://cslirary.stanford.edu/6( http://cslirary.stanford.edu/6) ) The companion text for the Binky video. Pointers and Memory: : (http://cslirary.stanford.edu/2( http://cslirary.stanford.edu/2) ) A 31 page explanation of everything you ever wanted to know aout pointers and memory. Linked List Basics: : (http://cslirary.stanford.edu/3( http://cslirary.stanford.edu/3) ) A 26 page introduction to the techniques and code for uilding linked lists s in C. Things to Rememer All declarations go at the eginning of each function. Only 0 and NULL evaluate to FALSE. All data is in memory. Each memory location has an address to use to refer to it and a value stored in it. A pointer is a High Level Language version of the address. * follows a pointer to its value & gets the address of a value 22440 Introduction to Microprocessors 27 22440 Introduction to Microprocessors 28