170 likes | 299 Views
ENEE150 Discussion #3 Week 2/17. Header Files. “.h” files Declares prototypes for 1 program #include “ backgammon.h ” Change the prototype in header file, changes the prototypes in all the respective files. Header Files. By convention: Don’t put actual implementation code in “.h” files
E N D
Header Files • “.h” files • Declares prototypes for 1 program • #include “backgammon.h” • Change the prototype in header file, changes the prototypes in all the respective files
Header Files • By convention: Don’t put actual implementation code in “.h” files • “.h” files need to be included in .c files • #include copy/pastes binary into your current file • Header Files include: • Function prototypes • Constants / Global Variables
Compiling Multiple .c Files • Compile 3 files, generate 3 .o files, links them into 1 executable • Ex: gccboard.ccheck.cbackgammon.croll_die.c
Extern Variables / Methods • Added to header files as a forward declaration of variable • Simple allocates space for the variable, but you still need to declare and create the variable • Can be declared any number of times, but only defined once Ex: extern int x;
Testing Project 1 Manual vs Automation • Manual: Enter inputs into the file 2. Automation: Redirect file input and output
Redirect Input/Output • 1. Use “<” to redirect standard input from a file Ex: sheet < sheet-test1.in • 2. Use “>” to redirect standard output to a file Ex: sheet < sheet-test1.in > output
Binary Conversions • Decimal to binary: • 15610 to 100111002 • 156/2= 78 R 0 • 78/2=39 R 0 • 39/2=19 R 1 • 19/2=9 R 1 • 9/2=4 R 1 • 4/2=2 R 0 • 2/2=1 R 0 • 1/2= 0 R 1
0x1B16to 110112 • Convert 1B16 to a decimal value: • 1*161+11*160=2710 • Convert 2710 to 110112 • 27/2= 13 R 1 • 13/2= 6 R 1 • 6/2= 3 R 0 • 3/2=1 R 1 • 1/2=0 R 1 • For Hex, you can go straight to decimal! • 1B: 1=0001, B=1011 : 00011011
What are Pointers? • Pointers are special variables that can hold the address of a variable. • Through pointers a developer can directly access memory from his/her code • But, as always, with great power comes great responsibility.
Pointers (Contd.) • A normal variable ‘var’ has a memory address of 1001 and holds a value 50. • A pointer variable has its own address 2047 but stores 1001, which is the address of the variable ‘var’
Pointer Declaration • A pointer is declared as : <pointer type> *<pointer-name> • pointer-type : It specifies the type of pointer. It can be int,char, float etc. This type specifies the type of variable whose address this pointer can store. • pointer-name : It can be any name specified by the user. Professionally, there are some coding styles which every code follows. The pointer names commonly start with ‘p’ or end with ‘ptr’
Example of Pointers • Pointers can be declared on the same line as other variables of the same variable type: intnum_var=1, *num_pointer; char letter=‘T’, *letter_pointer; num_pointer=&num_var; letter_pointer=&letter;
Use “ * “ before the pointer name to access its value. Question: Are pointers a two way street? intJay=63, int *tonight_show_host, *Jimmy; tonight_show_host=&Jay; printf(“The age of the last Tonight Show host is %i”, *tonight_show_host); Jimmy=tonight_show_host; *tonight_show_host=39; printf(“Jimmy Fallon’s age is %i, Jay Leno’s is %i, The Tonight Show host’s age is %i. \n”, *Jimmy, Jay, *tonight_show_host);