1 / 17

ENEE150 Discussion #3 Week 2/17

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

zelig
Download Presentation

ENEE150 Discussion #3 Week 2/17

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. ENEE150 Discussion #3 Week 2/17

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

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

  4. Compiling Multiple .c Files • Compile 3 files, generate 3 .o files, links them into 1 executable • Ex: gccboard.ccheck.cbackgammon.croll_die.c

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

  6. Testing Project 1 Manual vs Automation • Manual: Enter inputs into the file 2. Automation: Redirect file input and output

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

  8. ENEE150 Discussion #3 Week 2/10

  9. Binary Conversions

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

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

  12. Pointers

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

  14. 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’

  15. 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’

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

  17. 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);

More Related