Functions & Memory Maps Review C Programming Language
|
|
- Ruth Scott
- 6 years ago
- Views:
Transcription
1 Functions & Memory Maps Review C Programming Language Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department
2 Constants c 2
3 What Is A Constant? Constant a Value that cannot be altered by the program during normal execution, i.e., the value is constant. When associated with an Identifier, a constant is said to be "named," although the terms "constant" and "named constant" are often used interchangeably. # define PI 3.14 PI is a "named constant" in C/C++ The C/C++ Pre-Processor assigns the value to the constant with the pre-processor directive at the beginning of the program; their values can not be changed during the execution of the program! 3
4 Variables c 4
5 What Is A Variable? Variable a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a Value; these values can be changed during program execution. int Age, No = 5; Age is an integer Variable whose Value is unknown; some say the "garbage" value assigned is determined by the contents of memory at that instant in time. No is an integer Variable whose Value is known because this statement both creates and initializes the variable. 5
6 Datatyping c Static vs. Dynamic 6
7 Static & Dynamic Data Typing int No = 5; Static Datatyping once No is defined to be an integer, only integer data may be stored in the container. It is not an appropriate container in which to store a FullName. Static Datatyping Languages Include: Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML, Pascal, and Scala. Dynamic Datatyping the datatype is determined by whatever is assigned to the container at the moment; It may store a No at one moment and a FullName at the next. Dynamic Datatyping Languages Include: JavaScript, Python, Ruby, PHP, Lua and Perl. 7
8 Why c Functions? 8
9 List 3 Reasons Why We Need Functions 1] Reduce Duplication 2] Reduce Abstraction: Partition Task Into Major Ideas 3] Divide Task Into Smaller Portions That Can Be Written By A Team Of Programmers. 9
10 Recommended Ordering Of Components c Both C/C++ 10
11 Recommended Program Layout Order 1. Include Statements 2. Define Statements 3. Structs & Classes (classes are C++) 4. Function Prototypes 5. Functions In Some Type Of Logical Order (Including main at the top) Many Companies Require Developers To Declare Variables At Top 11
12 Function Size? c 12
13 How Big Shall You Make A Function? 1] 50 Lines Of Code vs. "No More Than Half Hour For An Experienced Computer Scientist To Understand The Complexity [ ½ Hr Better!] * Each Module Should Have Well Defined Task! * Write Them With Maintenance In Mind - Good Variables, Well Indented, Well Documented, Easy To Understand. [Solve General Problems!] 13
14 How Big Shall You Make A Function? void ClearScreen(void) system("cls"); Function ClearScreen, above, will clear the screen on a Windows System void ClearScreen(void) system("clear"); Short Functions Are Sometimes OK! Function ClearScreen, above, will clear the screen on a Mac Terminal or a Linux System Since the screen might have to be cleared more than 200 times within our Interactive System it would be a good idea to create the Function this would simplify the process of porting the code to the alternate system. 14
15 More General ClearScreen Solution void ClearScreen(void) #ifdef SYMANTEC_MAC // printf ("\f"); #endif //SYMANTEC_MAC //# define BORLAND_IBM #ifdef MICROSOFT_VISUAL // system("cls"); #endif //MICROSOFT_VISUAL //# define SYMANTEC_IBM //# define SYMANTEC_MAC #ifdef BORLAND_IBM // clrscr(); //# define LINUX #endif //BORLAND_IBM //# define CURSES //# define CODE_WARRIOR_IBM //# define CODE_WARRIOR_MAC # define MICROSOFT_VISUAL #ifdef LINUX // system("clear"); #endif //LINUX #ifdef CURSES // clear(); refresh(); #endif //CURSES Some Of You Would Might Prefer To Make One define Change which then makes all of the alterations necessary for the various operating systems. 15
16 I Am Going To Use Memory Maps To Review What c Happens With Memory In Main, Memory In Functions, Arguments Passed, etc. 16
17 Function HitCarriageReturnToContinue() Write A Function Which Will c Prompt The User To "Hit Return Key To Continue" Delay Processing Until The User Hits The Return Key 17
18 Function No Arguments No Explicit Return void HitCarriageReturnToContinue (void) char JunkString[5000]; flush_stream(stdin); printf(" <Hit Enter/Return Key To Continue> "); gets_s(junkstring); Called From main int main (int argc, char * argv[]) puts (" Start Of Main \n"); puts (" End Of Main \n"); HitCarriageReturnToContinue(); return (0); 18
19 c Examine What Happens When We Create An Un- Initialized Integer, Called No1, In Main. 19
20 About No1 (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) int No1 ;?? 101 What Is The Symbolic Name No1 What Value Is Currently Stored In No1 Garbage?? unknown Write An Assignment To Change The Value In No1 to 101 No1 = 101 Print The Value Contained In No1 printf ("No1 = %d\n", No1); Assume That The Address Where No1 Is Stored = Print The Address (base 10) Where No1 Is Stored printf ("&No1 = %ld\n", &No1); Print The Address (base 16) Where No1 Is Stored printf ("&No1 = %X\n", &No1); Print The Size (in bytes) of No1 printf ("sizeof( No1) = %ld\n", sizeof(no1)); No1 20
21 c Examine What Happens When We Create An Initialized Integer, Called No2, In Main. 21
22 About No2 (Assume Compiler Assigns Memory Starting At &1004 (Decimal) ) int main(int argc, char * argv[]) int No1, No2 = 12 ; 101 What Is The Symbolic Name No2 What Value Is Currently Stored In No2 12 Write An Assignment To Change The Value In No2 to 202 No2 = 202 Print The Value Contained In No2 printf ("No2 = %d\n", No2); Assume That The Address Where No2 Is Stored = &1004 Print The Address (base 10) Where No2 is Stored printf ("&No2 = %ld\n", &No2); Print The Address (base 16) Where No2 Is Stored printf ("&No2 = %X\n", &No2); Print The Size (in bytes) of No2 printf ("sizeof( No2) = %ld\n", sizeof(no2)); No1 12 & No2 22
23 Function DisplayAddition(Addend1, Addend2) Write A Function Which Will Display Addition Problem: Addend1 + Addend2 = Sum c 23
24 Function No Arguments No Explicit Return void DisplayAddition (int Addend1, int Addend2) int Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); Addend1 & Addend2 Are Passed By Value int main (int argc, char * argv[]) int No1 = 5, No2 = 6; DisplayAddition(No1, No2); DisplayAddition(3,4); Called From main Function DisplayAddition Has No Access To No1 Or No2 Unless Their Values Are Passed In As Arguments. 24
25 c DisplayAddition Exists, But Is Not Evoked/Called 25
26 [0] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; 5 No1 6 &1002 No2 void DisplayAddition (int Addend1, int Addend2) int Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); 26
27 c DisplayAddition Is Evoked/Called Pass Arguments By Value 27
28 [0] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; 5 No1 6 &1002 No2 DisplayAddition(No1, No2); void DisplayAddition (int Addend1, int Addend2) int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); 1 Function DisplayAddition Is Called 2 Addend1 Is Passed By Value Create The Short Int Container 5 &2000 Addend1 3 Place A Copy Of The Information In No1 (first argument match) Into The Container 28
29 [1] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; 5 No1 6 &1002 No2 DisplayAddition(No1, No2); void DisplayAddition (int Addend1, int Addend2) int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); 4 Addend2 Is Passed By Value Create The Short Int Container 5 & &2002 Addend1 Addend2 5 Place A Copy Of The Information In No2 (second argument match) Into The Container 29
30 [2] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); 5 void DisplayAddition (int Addend1, int Addend2) int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); 6 Create The Short Int Container For Sum No1 6 No2 &1002?? Sum & Addend1 & Addend2 &
31 [3] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); 5 void DisplayAddition (int Addend1, int Addend2) int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); 7 Place Addend1 (5) + Addend2 (6) into Sum No1 6 No2 &1002?? 11 Sum & Addend1 & Addend2 &
32 [4] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); 5 void DisplayAddition (int Addend1, int Addend2) int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); 8 The Print Output Goes To The Terminal Console No1 6 No2 &1002?? 11 Sum & Addend1 & Addend2 &
33 [5] DisplayAdditon (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[ ]) No1 = 5, No2 = 6 ; DisplayAddition(No1, No2); 5 void DisplayAddition (int Addend1, int Addend2) int Sum; Sum = Addend1 + Addend2; printf("%d + %d = %d\n", Addend1, Addend2, Sum); No1 9 At The End Of The Function All Of The Memory Allocated For Arguments & Local Variables Is Returned To The Operating System For Reuse Addend1, Addend2, & Sum Cease To Exist! 11 & & & &1002 Sum No2 Addend1 Addend2 33
34 Function SwapA (A,B) Write A Function Which Will Swap The Contents Of A & B c THIS ATTEMP FAILS! 34
35 Function No Arguments No Explicit Return void SwapA ( A, B) Temp; Temp = A; A = B; B = Temp; int main (int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); A & B Are Passed By Value Called From main Function SwapA Has No Access To No1 Or No2 Unless Their Values Are Passed In As Arguments. SwapA ( No1, No2); 35
36 c SwapA Is Evoked/Called Pass Arguments By Value 36
37 [1] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 void SwapA ( A, B) Temp; Temp = A; A = B; B = Temp; 37
38 [2] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapA ( No1, No2); No1 No1 = 5 No2 = 12 V A Is It Passed By Value Or By Reference? 5 12 &1002 No2 Reference int * A Int & A Value Int A void SwapA ( A, B) Temp; Temp = A; A = B; B = Temp; 38
39 [3] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapA ( No1, No2); Since A Is By Reference Create A Variable Whose Symbolic Name Is A And Fill It With The Value Of The First Argument In The Function Call. void SwapA ( A, B) Temp; 5 A &1004 Temp = A; A = B; B = Temp; 39
40 [4] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapA ( No1, No2); Since B Is By Reference Create An int Variable Whose Symbolic Name Is B And Fill It With The Value Of The Second Argument In The Function Call. void SwapA ( A, B) Temp; 5 A & B &1006 Temp = A; A = B; B = Temp; 40
41 [5] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapA ( No1, No2); Temp is a local variable will be destroyed when function is finished. Create An int Variable Whose Symbolic Name Is Temp Contains Garbage void SwapA ( A, B) Temp; 5 A & B &1006? Temp &1008 Temp = A; A = B; B = Temp; 41
42 [6] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapA ( No1, No2); Function SwapA can access it s parameters (A & B), global variables (bad practice), defines, and it s local variables (Temp). No1 = 5 No2 = 12 void SwapA ( A, B) Temp; printf ("No2 = %d\n", No2); 5 A & B &1006? Temp &1008 Temp = A; A = B; B = Temp; 42
43 [7] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); SwapA ( No1, No2); No2 = 12 void SwapA ( A, B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); A = 5 B = 12 Temp = A; A = B; B = Temp; 5 A & B &1006? Temp &1008 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 43
44 [8] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); SwapA ( No1, No2); No2 = 12 void SwapA ( A, B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); A = 5 B = 12 Temp = A; A = B; B = Temp; 5 A & B &1006? 5 Temp &1008 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 44
45 [9] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); SwapA ( No1, No2); No2 = 12 void SwapA ( A, B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); A = 5 B = 12 Temp = A; A = B; B = Temp; 5 12 A & B &1006? 5 Temp &1008 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 45
46 [10] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); SwapA ( No1, No2); No2 = 12 void SwapA ( A, B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); A = 5 B = 12 Temp = A; A = B; B = Temp; 5 12 &1004 A 12 5 &1006 B? 5 Temp &1008 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 46
47 [11] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); SwapA ( No1, No2); No2 = 12 void SwapA ( A, B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); A = 5 B = 12 Temp = A; A = B; B = Temp; 5 12 &1004 A 12 5 &1006 B? 5 Temp &1008 printf ("A = %d\n", A); A = 12 printf ("B = %d\n", B); B = 5 printf ("Temp = %d\n", Temp); 47
48 [12] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); SwapA ( No1, No2); No2 = 12 void SwapA ( A, B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); A = 5 B = 12 Temp = A; A = B; B = Temp; 5 12 &1004 A 12 5 &1006 B? 5 Temp &1008 printf ("A = %d\n", A); A = 12 printf ("B = %d\n", B); B = 5 printf ("Temp = %d\n", Temp); 48
49 [13] SwapA (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapA ( No1, No2); printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); No2 = 12 void SwapA ( A, B) Temp; Temp = A; A = B; B = Temp; 49
50 Passing Arguments By Value The function receives a copy of the actual arguments passed to the function as parameters. The function can change the value of the parameter, but cannot change the values of the actual actual arguments. 50
51 Function SwapB (A,B) Write A Function Which Will Swap The Contents Of A & B c THIS ATTEMP WORKS! 51
52 Function No Arguments No Explicit Return void SwapB ( * A, * B) Temp; Temp = *A; *A = *B; *B = Temp; A & B Are Passed By Reference int main (int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); Called From main Since Function SwapB Uses Pointers This Swap Will Be Able To Change The Values Of No1 & No2. 52
53 c SwapB Is Evoked/Called Pass Arguments By Reference POINTERS! 53
54 [1] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); No1 = 5 printf ("No2 = %d\n", No2); No2 = 12 void SwapB ( * A, * B) Temp; Temp = *A; *A = *B; *B = Temp; 54
55 [2] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); No1 R A Is It Passed By Value Or By Reference? 5 No1 = 5 No2 = &1002 No2 Reference int * A Int & A Value Int A The Only Way To Change No1 Is To Use A Pointer Variable Or A Reference Variable; We Are Using A Standard C Pointer Variable In This Example! void SwapB ( * A, * B) Temp; Temp = *A; *A = *B; *B = Temp; 55
56 [3] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapB ( &No1, &No2); Store &No1 In Pointer A Since A Is By Reference Create A Pointer To A Variable Whose Symolic Name Is A And Fill It With The Address Passed In The First Argument In The Function Call. void SwapB ( * A, * B) Temp; &1004 A Ptrs Are 4 Bytes Temp = *A; *A = *B; *B = Temp; 56
57 [4] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapB ( &No1, &No2); Store &No2 In Pointer B Since B Is By Reference Create A Pointer To A Variable Whose Symolic Name Is B And Fill It With The Address Passed In The Second Argument In The Function Call. void SwapB ( * A, * B) Temp; &1004 A &1002 &1008 B Temp = *A; *A = *B; *B = Temp; 57
58 [5] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapB ( &No1, &No2); Temp is a local variable will be destroyed when function is finished. Create An int Variable Whose Symolic Name Is Temp Contains Garbage void SwapB ( * A, * B) Temp; &1004 A &1002 &1008 B? Temp &1012 Temp = *A; *A = *B; *B = Temp; 58
59 [6] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; 5 No1 12 &1002 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No1 = 5 No2 = 12 SwapB ( &No1, &No2); Function SwapB can access it s parameters (A & B), global variables (bad practice), defines, and it s local variables (Temp). void SwapB ( * A, * B) Temp; printf ("No2 = %d\n", No2); &1004 A &1002 &1008 B? Temp &1012 Temp = *A; *A = *B; *B = Temp; 59
60 [7] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); 5 No1 = 5 No2 = 12 No1 12 &1002 No2 void SwapB ( * A, * B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("*A = %d\n", *A); printf ("*B = %d\n", *B); A = 1000 B = 1002 *A = 5 *B = 12 Temp = *A; *A = *B; *B = Temp; &1004 A &1002 &1008 B? Temp &1012 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 60
61 [8] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); 5 No1 = 5 No2 = 12 No1 12 &1002 No2 void SwapB ( * A, * B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("*A = %d\n", *A); printf ("*B = %d\n", *B); A = 1000 B = 1002 *A = 5 *B = 12 Temp = *A; *A = *B; *B = Temp; &1004 A &1002 &1008 B? 5 Temp &1012 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 61
62 [9] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); 5 12 No1 No2 No1 = 5 No2 = 12 void SwapB ( ** A, A, ** B) Temp; 12 &1002 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("*A = %d\n", *A); printf ("*B = %d\n", *B); A = 1000 B = 1002 *A = 5 *B = 12 Temp = *A; *A = *B; *B = Temp; &1004 A &1002 &1008 B? 5 Temp &1012 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); 62
63 [10] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); 5 12 No No2 &1002 No1 = 5 No2 = 12 void void SwapB ( int ** A, A, short int ** B) B) short int Temp; printf printf ("A ("A = %d\n", A); A); printf printf ("B ("B = %d\n", B); B); printf printf ("*A ("*A = %d\n", *A); *A); printf printf ("*B ("*B = %d\n", *B); *B); A = 1000 B = 1002 *A = 5 *B = 12 Temp Temp = *A; *A; *A *A = *B; *B; *B *B = Temp; &1004 A &1002 &1008 B? 5 Temp &1012 printf printf ("A ("A = %d\n", A); A); printf printf ("B ("B = %d\n", B); B); printf printf ("Temp = %d\n", Temp); 63
64 [11] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); 5 No1 = 5 No2 = No &1002 No2 void SwapB ( * A, * B) Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("*A = %d\n", *A); printf ("*B = %d\n", *B); A = 1000 B = 1002 *A = 5 *B = 12 Temp = *A; *A = *B; *B = Temp; &1004 A &1002 &1008 B? 5 Temp &1012 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); A = 1000 B = 1002 Temp = 5 64
65 [12] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB ( &No1, &No2); 5 12 No &1002 No1 = 5 void SwapB ( * A, * B) No2 = 12 Temp; printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("*A = %d\n", *A); printf ("*B = %d\n", *B); No2 A = 1000 B = 1002 *A = 5 *B = 12 Temp = *A; *A = *B; *B = Temp; &1004 A &1002 &1008 B? 5 Temp &1012 printf ("A = %d\n", A); printf ("B = %d\n", B); printf ("Temp = %d\n", Temp); A = 1000 B = 1002 Temp = 5 65
66 [13] SwapB (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5, No2 = 12 ; printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); SwapB( No1, No2); 5 12 No &1002 No1 = 5 No2 = 12 No2 printf ("No1 = %d\n", No1); printf ("No2 = %d\n", No2); No2 = 12 No1 = 5 void SwapB ( * A, * B) Temp; Temp = *A; *A = *B; *B = Temp; 66
67 Passing Arguments By Reference Very useful when we need a function which "returns more than one value". The formal parameter becomes an alias (a pointer to) for the actual parameter. The function can change the values of the actual arguments. 67
68 Function Square (No1) Write A Function Which Will c Explicitly Return The Square Of No1 68
69 Function No Arguments No Explicit Return long int Square (long int No1) long Product = No1 * No1; return (Product); Called From main int main(int argc, char * argv[]) long int No1 = 5; No1 Is Passed By Value printf ("Square(No1) = %d\n", Square(No1)); printf("square(10) = %ld\n", Square(10)); No1 = Square(12); printf("no1 = %ld\n", No1); 69
70 c SwapB Is Evoked/Called Pass Arguments By Reference POINTERS! 70
71 [1] Square (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) long int No1 = 5; 5 No1 printf ("No1 = %ld\n", No1); No1 = 5 long int Square (long int No1) return (No1 * No1); 71
72 [2] Square (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) long int No1 = 5; 5 No1 printf ("No1 = %ld\n", No1); No1 = 5 printf("square(no1) = %ld\n", Square(No1)); Create A long int Variable Whose Symbolic Name Is No1 And Fill It With The Value Of The First Argument In The Function Call. 5 &1004 No1 long int Square (long int No1) long Product = No1 * No1; return (Product); 72
73 [3] Square (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) long int No1 = 5; 5 No1 printf ("No1 = %ld\n", No1); No1 = 5 printf("square(no1) = %ld\n", Square(No1)); Create A long int Variable Whose Symbolic Name Is Product And Fill It With The No1 * No1 5 No1 & &1008 Product long int Square (long int No1) long Product = No1 * No1; return (Product); 73
74 [4] Square (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) long int No1 = 5; 5 No1 printf ("No1 = %ld\n", No1); No1 = 5 printf("square(no1) = %ld\n", Square(No1)); Return The Product to each and every place where Function Square Is Evoked/Called 5 &1004 No1 25 &1008 Product long int Square (long int No1) long Product = No1 * No1; return (Product); 74
75 [2] Square (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) long int No1 = 5; 5 No1 printf ("No1 = %ld\n", No1); No1 = 5 printf("square(no1) = %ld\n", Square(No1)); Displays 25 On Terminal Create A long int Variable Whose Symbolic Name Is Product And Fill It With The No1 * No1 5 No1 & &1008 Product long int Square (long int No1) long Product = No1 * No1; return (Product); 75
76 [5] Square (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) long int No1 = 5; 5 No1 printf("square(no1) = %ld\n", Square(No1)); printf("square(10) = %ld\n", Square(10)); No1 = Square(12); Fills No1 With 144 Displays 25 On Terminal Displays 100 On Terminal printf("no1 = %ld\n", No1); Displays 144 On Terminal Return The Product to each and every place where Function Square Is Evoked/Called 144 &1004 No1 25 &1008 Product long int Square (long int No1) long Product = No1 * No1; return (Product); 76
77 Function Increment (No1) Write A Function Which Will Add 1 To The Short Integer Whose Address Is Passed c REFERENCE VARIABLE Preview Of Things To Come C++ 77
78 Function No Arguments No Explicit Return void Increment ( & No1) No1++; No1 Is Passed By Reference int main(int argc, char * argv[]) No1 = 5; printf ("No1 = %d\n", No1); Increment(No1); Called From main Function Increment Has No Access To No1 Unless It's Values Are Passed In As Arguments. 78
79 c Increment Is Evoked/Called Preview Of C++ Reference Variables 79
80 [0] Increment (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5; printf ("No1 = %d\n", No1); 5 No1 No1 = 5 void Increment ( & No1) No1++; 80
81 [1] Increment (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5; 5 No1 printf ("No1 = %d\n", No1); No1 = 5 Increment(No1); Store &No1 In Pointer No1 &10042 No1 void Increment ( & No1) No1++; 81
82 [2] Increment (Assume Compiler Assigns Memory Starting At (Decimal) ) int main(int argc, char * argv[]) No1 = 5; 5 6 No1 printf ("No1 = %d\n", No1); No1 = 5 Increment(No1); printf ("No1 = %d\n", No1); No1 = 6 void Increment ( & No1) No1++; 82
83 Passing Arrays To c Functions? 83
84 Arrays Are Always Passed As Pointers Sizeof Cannot Be Used To Determine Capacity void FillIntArray(int Array[], int StartingNo, int NoElements) printf("fillintarray--> sizeof(array)= %ld\n", sizeof(array)); Array 5 StartingNo 4 NoElements &2000 &2004 &2008 int main (int argc, char * argv[]) int Nos[4]; I Have To Pass The NoToFill In Order To Know How Many Elements To Put Into Array printf("main--> sizeof(nos)= %ld\n", sizeof(nos)); FillIntArray(Nos, 5, 4); 20 bytes???????? Nos 84
85 Arrays Are Always Passed As Pointers Function Call Did Not Include &Nos? void FillIntArray(int Array[], int StartingNo, int NoElements) printf("fillintarray--> Array = %ld\n\n", Array); & Array 5 StartingNo 4 NoElements &2000 &2004 &2008 int main (int argc, char * argv[]) int Nos[4]; Note That The int Array[] Parameter Does Not Have A Value Such As [4] printf("fillintarray--> Array = %ld\n\n", Array); FillIntArray(Nos, 5, 4); 20 bytes???????? & Nos 85
86 Arrays Are Always Passed As Pointers Function Call Could Pass &Nos[0] void FillIntArray(int Array[], int StartingNo, int NoElements) printf("fillintarray--> Array = %ld\n\n", Array); & Array 5 StartingNo 4 NoElements &2000 &2004 &2008 int main (int argc, char * argv[]) 20 bytes int Nos[4]; printf("fillintarray--> Array = %ld\n\n", Array); FillIntArray(&Nos[0], 5, 4);???????? & Nos 86
87 Arrays Are Passed By Reference Arrays may have thousands/millions of elements; it would slow the program down if the compiler automatically copied all of the elements. We can manually make a copy of those elements in the small number of situations that this would be required. Sizeof cannot be used to determine capacity. The capacity/max will have to be passed or included in some type of struct or class. 87
88 Arrays Are Always Passed As Pointers Changing Nos[0] void FillIntArray(int Array[], int StartingNo, int NoElements) Array[0] = StartingNo; & Array 5 StartingNo 4 NoElements &2000 &2004 &2008 int main (int argc, char * argv[]) int Nos[4]; I Don't Want You To Take It On Faith That The Change In Nos[0] Has Occurred! FillIntArray(Nos, 5, 4); 20 bytes???????? 5 & Nos 88
89 void DisplayIntArray(int Array[], int NoElements) for (int Pos = NoElements - 1; Pos >= 0; Pos--) printf(" \n"); printf("%4ld %10ld \n", Pos, Array[Pos]); printf(" Arrays Are Always Passed As Pointers Confirm The Change Not A Very Good Display! \n\n"); int main (int argc, char * argv[]) int Nos[4]; FillIntArray(Nos, 5, 4); DisplayIntArray(Nos, 4); void FillIntArray(int Array[], int StartingNo, int NoElements) Array[0] = StartingNo;
90 Arrays Are Always Passed As Pointers Finish My FillIntArray void FillIntArray(int Array[], int StartingNo, int NoElements) for (int Pos = 0-1; Pos < NoElements; Pos++) Array[Pos] = StartingNo + Pos; void FillIntArray(int Array[], int StartingNo, int NoElements) Objective: Place StartingNo in Element 0 StartingNo + 1 in Element 1 StartingNo + 2 in Element 2 Continue Until All Elements Have Been Filled int main (int argc, char * argv[]) int Nos[4]; FillIntArray(Nos, 5, 4); DisplayIntArray(Nos, 4); 90
91 Character Strings Are Passed By Reference Character Strings are Arrays Of Character Character Strings Are Passed By Reference void ToLowerCase (char String[]) long int Pos; for (Pos = 0; Pos <= strlen(string); Pos ++) if ((String[Pos] >= 'A') && (String[Pos] <= 'Z')) String[Pos] = String[Pos] + 32; 91
92 c 92
93 A Complete Program - 1 // Includes # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; 93
94 A Complete Program - 2 // Includes # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; // Defines # define PI
95 A Complete Program - 3 // Includes # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; // Defines # define PI 3.14 // Structs & Classes struct Student char FirstName[15], LastName[20]; long No; ; 95
96 A Complete Program - 4 // Includes # include <iostream> # include <fstream> # include <iomanip> # include <stdio.h> # include <string.h> # include <stdlib.h> using namespace std; // Defines # define PI 3.14 // Structs & Classes struct Student char FirstName[15], LastName[20]; long No; ; // Function Prototypes void HitCarriageReturnToContinue(void); void Set(Student * ThisStudent, char NewFirst[], char NewLast[], long NewNo); void Display(Student ThisStudent); 96
97 A Complete Program - 5 // Main & The Other Functions int main (int argc, char * argv[]) puts(" Start Of Main \n"); Student Jane; Set(&Jane, "Jane", "Doe", 1234); Display(Jane); puts (" HitCarriageReturnToContinue(); return (0); End Of Main \n"); 97
98 A Complete Program - 6 // Main & The Other Functions void Set(Student * ThisStudent, char NewFirst[], char NewLast[], long NewNo) ThisStudent->No = NewNo; strcpy_s(thisstudent->firstname, NewFirst); strcpy_s(thisstudent->lastname, NewLast); void Display(Student ThisStudent) printf("no = %d\n", ThisStudent.No); printf("first = %s\n", ThisStudent.FirstName); printf("second = %s\n\n", ThisStudent.LastName); 98
Function Terminology
OOP-1-Review-HW-(Part C).docx CSCI 2320 Initials P a g e 1 Print Name Time Required =. Hrs. Signature (pledged) Function Terminology 1] A C_?_ is a Value that cannot be altered by the program during normal
More informationPointers & Dynamic Memory Review C Pointers Introduce C++ Pointers
Pointers & Dynamic Memory Review C Pointers Introduce C++ Pointers Data Abstractions CSCI-2320 Dr. Tom Hicks Computer Science Department c http://carme.cs.trinity.edu/ thicks/2320/schedule.html http://carme.cs.trinity.edu/thicks/2320/schedule.html
More informationRECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!
1 2 RECOMMENDATION Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 2 Function Overloading C++ provides the capability of Using
More informationCall The Project Dynamic-Memory
1 2 2 Call The Project Dynamic-Memory 4 4 Copy-Paste Main # include "Utilities.hpp" int main(int argc, char * argv[]) { short int *PtrNo; (*PtrNo) = 5; printf ("(*PtrNo) = %d\n", (*PtrNo)); } getchar();
More informationFrom Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides
From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor
More informationOutline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C
Lecture 1 C primer What we will cover A crash course in the basics of C You should read the K&R C book for lots more details Various details will be exemplified later in the course Outline Overview comparison
More informationMemory Allocation in C
Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called
More informationCA341 - Comparative Programming Languages
CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationRicardo Rocha. Department of Computer Science Faculty of Sciences University of Porto
Ricardo Rocha Department of Computer Science Faculty of Sciences University of Porto Adapted from the slides Revisões sobre Programação em C, Sérgio Crisóstomo Compilation #include int main()
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationmith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut
mith College CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 2 Hours D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard
More informationArrays and Pointers in C. Alan L. Cox
Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including
More informationLecture 16. Daily Puzzle. Functions II they re back and they re not happy. If it is raining at midnight - will we have sunny weather in 72 hours?
Lecture 16 Functions II they re back and they re not happy Daily Puzzle If it is raining at midnight - will we have sunny weather in 72 hours? function prototypes For the sake of logical clarity, the main()
More informationChapter 11 Introduction to Programming in C
C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University! Gives
More informationRunning a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2
CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with
More informationCSC231 C Tutorial Fall 2018 Introduction to C
mith College CSC231 C Tutorial Fall 2018 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 4 Installments! Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to Assembly Unix Standard
More information# 1. Objectives. Objectives. 13.Visual Studio Projects. C/C++ The array is an Aggregate!
1 2 Objectives 1. Agregates 2. Structs 3. Introduction To Classes 4. Constructor 5. Destructor 6. Function Overloading 7. Default Arguments 8. Accessors & Mutators 9. ADT Abstract Data Types 10. Operator
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University C: A High-Level Language! Gives
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations
More informationFirst of all, it is a variable, just like other variables you studied
Pointers: Basics What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the address (rather than the value)
More informationCS 261 Data Structures. Introduction to C Programming
CS 261 Data Structures Introduction to C Programming Why C? C is a lower level, imperative language C makes it easier to focus on important concepts for this class, including memory allocation execution
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationUNIT V Sub u P b ro r g o r g a r m a s
UNIT V SubPrograms Outline Subprograms Parameter Passing Parameter correspondence Main Issues when designing subroutine in programming languages Parameter passing techniques Characteristics of Subprogram
More informationOnline Judge and C. Roy Chan. January 12, Outline Information Online Judge Introduction to C. CSC2100B Data Structures Tutorial 1
Roy Chan CSC2100B Data Structures Tutorial 1 January 12, 2009 1 / 38 1 Information Your TA team Course Information Assignment 2 Online Judge Writing Your Assignment Program Submitting Your Program Online
More informationFinal CSE 131B Spring 2004
Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)
More informationmith College Computer Science CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut
mith College CSC231 Bash Labs Week #10, 11, 12 Spring 2017 Introduction to C Dominique Thiébaut dthiebaut@smith.edu Learning C in 4 Hours! D. Thiebaut Dennis Ritchie 1969 to 1973 AT&T Bell Labs Close to
More informationCS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco
CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C C: A High-Level Language Gives symbolic names to values don t need to know which register or memory location Provides abstraction of underlying hardware operations
More informationECE 250 / CPS 250 Computer Architecture. C Programming
ECE 250 / CPS 250 Computer Architecture C Programming Benjamin Lee Slides based on those from Andrew Hilton (Duke), Alvy Lebeck (Duke) Benjamin Lee (Duke), and Amir Roth (Penn) Outline Previously:! Computer
More informationMidterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009
Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009 April 16, 2009 Instructions: Please write your answers on the printed exam. Do not turn in any extra pages. No interactive electronic devices
More informationIntroduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers
Introduction to C Geared toward programmers Robert Escriva Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Cornell CS 4411, August 30, 2010 1 Why C? 2 A Quick Example 3 Programmer s Responsibilities
More informationSubject: Fundamental of Computer Programming 2068
Subject: Fundamental of Computer Programming 2068 1 Write an algorithm and flowchart to determine whether a given integer is odd or even and explain it. Algorithm Step 1: Start Step 2: Read a Step 3: Find
More informationArrays and Pointers. CSE 2031 Fall November 11, 2013
Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example
More informationECE 250 / CS 250 Computer Architecture. C Programming
ECE 250 / CS 250 Computer Architecture C Programming Benjamin Lee Some slides based on those from Alvin Lebeck, Daniel Sorin, Andrew Hilton, Amir Roth, Gershon Kedem Outline Previously: Computer is a machine
More information# 1. Objectives. Dangling Pointers FirstName & LastName - Pointers Reference Memory Incorrect Memory! Not A Good Constructor!
Objectives. Dynamic Memory. Shallow Copy. Deep Copy. LIFO & FIFO. Array Implementation Of A Stack 6. Dynamic Stack 7. Templates 8. Template Stack 9. Primitive Operations Push, Pop, Empty Full, Resize,
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationCourse organization. Course introduction ( Week 1)
Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and
More informationLanguage comparison. C has pointers. Java has references. C++ has pointers and references
Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified
More informationWhere do we go from here?
Where do we go from here? C++ classes and objects, with all the moving parts visible operator overloading templates, STL, standards, Java components, collections, generics language and performance comparisons
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,
More informationChapter 11 Introduction to Programming in C
Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified by Chris Wilcox, Yashwant Malaiya Colorado State University C: A High-Level Language
More informationToday s class. Review of more C Operating system overview. Informationsteknologi
Today s class Review of more C Operating system overview Monday, September 10, 2007 Computer Systems/Operating Systems - Class 3 1 Review of more C File handling Open a file using fopen Returns a file
More informationMemory management. Johan Montelius KTH
Memory management Johan Montelius KTH 2017 1 / 22 C program # include int global = 42; int main ( int argc, char * argv []) { if( argc < 2) return -1; int n = atoi ( argv [1]); int on_stack
More informationDeep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems
Deep C Multifile projects Getting it running Data types Typecasting Memory management Pointers Fabián E. Bustamante, Fall 2004 Multifile Projects Give your project a structure Modularized design Reuse
More informationChapter 11 Introduction to Programming in C
C: A High-Level Language Chapter 11 Introduction to Programming in C Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Gives
More informationArrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.
Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example
More informationA Quick Look at C for C++ Programmers
COMP 40: Machine Structure and Assembly Language Programming (Fall 2017) A Quick Look at C for C++ Programmers Noah Mendelsohn (with updates by Mark Sheldon) Tufts University Email: noah@cs.tufts.edu Web:
More informationProgrammazione. Prof. Marco Bertini
Programmazione Prof. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Hello world : a review Some differences between C and C++ Let s review some differences between C and C++ looking
More informationChapter 2. Procedural Programming
Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic
More informationBasic and Practice in Programming Lab7
Basic and Practice in Programming Lab7 Variable and Its Address (1/2) What is the variable? Abstracted representation of allocated memory Having address & value Memory address 10 0x00000010 a int a = 10;
More informationCE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings
CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location
More informationIntroduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers
Introduction to C Geared toward programmers Ayush Dubey Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Cornell CS 4411, August 31, 2012 Administrative Information Outline
More informationRECOMMENDATION. Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function!
1 RECOMMENDATION Don't Write Entire Programs Unless You Want To Spend 3-10 Times As Long Doing Labs! Write 1 Function - Test That Function! 2 3 Copy Project Folder There will be a number of times when
More informationEvolution of Programming Languages
Evolution of Programming Languages 40's machine level raw binary 50's assembly language names for instructions and addresses very specific to each machine 60's high-level languages: Fortran, Cobol, Algol,
More informationIntroduction to Programming Block Tutorial C/C++
Michael Bader Lehrstuhl Informatik V bader@in.tum.de March, 4th March, 8th, 2002 Abstract This is where an abstract might go if you want one. There is usually not a lot of room for much here. C/C++ Tutorial
More informationPointers. Introduction
Pointers Spring Semester 2007 Programming and Data Structure 1 Introduction A pointer is a variable that represents the location (rather than the value) of a data item. They have a number of useful applications.
More informationProgramming in C. Pointers and Arrays
Programming in C Pointers and Arrays NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Pointers and Arrays In C, there is a strong relationship
More informationModifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }
Modifiers unsigned. For example unsigned int would have a range of [0..2 32 1] on a 32-bit int machine. const Constant or read-only. Same as final in Java. static Similar to static in Java but not the
More informationCS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline
CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can
More informationPrograms. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems
Programs CSCI 4061 Introduction to Operating Systems C Program Structure Libraries and header files Compiling and building programs Executing and debugging Instructor: Abhishek Chandra Assume familiarity
More informationCSE 303 Midterm Exam
CSE 303 Midterm Exam October 29, 2008 Name Sample Solution The exam is closed book, except that you may have a single page of hand written notes for reference. If you don t remember the details of how
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationIntroduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers
Introduction to C Geared toward programmers Sean Ogden Slide heritage: Alin Dobra Niranjan Nagarajan Owen Arden Robert Escriva Zhiyuan Teo Ayush Dubey Cornell CS 4411, August 30, 2013 Administrative Information
More informationCS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1. W hat are Keywords? Keywords are certain reserved words that have standard and pre-defined meaning in C. These keywords can be used only for their
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationCS 376b Computer Vision
CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves
More informationCS 241 Data Organization Pointers and Arrays
CS 241 Data Organization Pointers and Arrays Brooke Chenoweth University of New Mexico Fall 2017 Read Kernighan & Richie 6 Structures Pointers A pointer is a variable that contains the address of another
More informationOrganization of Programming Languages CS 3200/5200N. Lecture 09
Organization of Programming Languages CS 3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Control Flow Control flow = the flow of control, or execution
More informationECE264 Summer 2013 Exam 1, June 20, 2013
ECE26 Summer 2013 Exam 1, June 20, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it. I
More informationC: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36
Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar
More informationMidterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.
Midterm Exam Nov 8th, 2012 COMS W3157 Advanced Programming Columbia University Fall 2012 Instructor: Jae Woo Lee About this exam: - There are 4 problems totaling 100 points: problem 1: 30 points problem
More informationECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee
ECE 250 / CS 250 Computer Architecture C to Binary: Memory & Data Representations Benjamin Lee Slides based on those from Alvin Lebeck, Daniel Sorin, Andrew Hilton, Amir Roth, Gershon Kedem Administrivia
More informationMemory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)
Memory Allocation Memory What is memory? Storage for variables, data, code etc. How is memory organized? Text (Code) Data (Constants) BSS (Global and static variables) Text Data BSS Heap Stack (Local variables)
More informationA506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).
A506 / C201 Computer Programming II Placement Exam Sample Questions For each of the following, choose the most appropriate answer (2pts each). 1. Which of the following functions is causing a temporary
More informationChapter 9 Subprograms
Chapter 9 Subprograms We now explore the design of subprograms, including parameter-passing methods, local referencing environment, overloaded subprograms, generic subprograms, and the aliasing and problematic
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 14, SPRING 2013 RECAP ARRAYS VS. POINTERS C Parameter Passing Notes We'll say formal parameter vs actual parameter. Formal
More informationProgram Organization and Comments
C / C++ PROGRAMMING Program Organization and Comments Copyright 2013 Dan McElroy Programming Organization The layout of a program should be fairly straight forward and simple. Although it may just look
More informationA Crash Course in C. Steven Reeves
A Crash Course in C Steven Reeves This class will rely heavily on C and C++. As a result this section will help students who are not familiar with C or who need a refresher. By the end of this section
More informationTutorial 1: Introduction to C Computer Architecture and Systems Programming ( )
Systems Group Department of Computer Science ETH Zürich Tutorial 1: Introduction to C Computer Architecture and Systems Programming (252-0061-00) Herbstsemester 2012 Goal Quick introduction to C Enough
More informationIntroduction to C++ Introduction to C++ 1
1 What Is C++? (Mostly) an extension of C to include: Classes Templates Inheritance and Multiple Inheritance Function and Operator Overloading New (and better) Standard Library References and Reference
More informationAgenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures
The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF
More informationProblem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.
Problem 1 Recall the definition of root in project 1. (The declaration of struct entrynode appears below.) struct entrynode * root; Give the type of each of the following expressions. The answer may be
More informationCSE 303 Lecture 8. Intro to C programming
CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, 2.2-2.4, 2.6, 3.1, 5.1, 7.1-7.2, 7.5.1-7.5.4, 7.6-7.9, Ch. 8; Programming in C Ch. 1-6 slides created by Marty Stepp http://www.cs.washington.edu/303/
More informationFunctions in C C Programming and Software Tools. N.C. State Department of Computer Science
Functions in C C Programming and Software Tools N.C. State Department of Computer Science Functions in C Functions are also called subroutines or procedures One part of a program calls (or invokes the
More informationChapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016
Chapter 6: User-Defined Functions Objectives In this chapter, you will: Learn about standard (predefined) functions Learn about user-defined functions Examine value-returning functions Construct and use
More informationCSE 12 Spring 2016 Week One, Lecture Two
CSE 12 Spring 2016 Week One, Lecture Two Homework One and Two: hw2: Discuss in section today - Introduction to C - Review of basic programming principles - Building from fgetc and fputc - Input and output
More informationChapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses
Chapter 16 Pointers and Arrays Based on slides McGraw-Hill Additional material 200/2005 Lewis/Martin Pointers and Arrays We've seen examples of both of these in our LC- programs; now we'll see them in
More informationEMBEDDED SYSTEMS PROGRAMMING Language Basics
EMBEDDED SYSTEMS PROGRAMMING 2014-15 Language Basics (PROGRAMMING) LANGUAGES "The tower of Babel" by Pieter Bruegel the Elder Kunsthistorisches Museum, Vienna ABOUT THE LANGUAGES C (1972) Designed to replace
More informationPointers and scanf() Steven R. Bagley
Pointers and scanf() Steven R. Bagley Recap Programs are a series of statements Defined in functions Can call functions to alter program flow if statement can determine whether code gets run Loops can
More informationTopic 6: A Quick Intro To C
Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions
More informationAgenda / Learning Objectives: 1. Map out a plan to study for mid-term Review the C++ operators up to logical operators. 3. Read about the tips
Agenda / Learning Objectives: 1. Map out a plan to study for mid-term 2. 2. Review the C++ operators up to logical operators. 3. Read about the tips and pitfalls on using arrays (see below.) 4. Understand
More informationFunctions. Arash Rafiey. September 26, 2017
September 26, 2017 are the basic building blocks of a C program. are the basic building blocks of a C program. A function can be defined as a set of instructions to perform a specific task. are the basic
More informationOOP-1-Review-HW-(Part B).docx CSCI 2320 Initials P a g e 1. Print Name Time Required =. Hrs. Signature (pledged) Static Array Review
OOP-1-Review-HW-(Part B).docx CSCI 2320 Initials P a g e 1 Print Name Time Required =. Hrs. Signature (pledged) Static Array Review 1] Write the line of C/C++ code to create an array, called Nos1, which
More informationScientific Computing
Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course
More informationECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.
ECE 264 Exam 2 6:30-7:30PM, March 9, 2011 I certify that I will not receive nor provide aid to any other student for this exam. Signature: You must sign here. Otherwise you will receive a 1-point penalty.
More informationC: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/21 Pointers A pointer is a variable that stores the address of another variable. Pointers are similar to
More informationCOMP322 - Introduction to C++
COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments
More informationECE264 Fall 2013 Exam 3, November 20, 2013
ECE264 Fall 2013 Exam 3, November 20, 2013 In signing this statement, I hereby certify that the work on this exam is my own and that I have not copied the work of any other student while completing it.
More informationVariables Data types Variable I/O. C introduction. Variables. Variables 1 / 14
C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition
More information