DOC PREVIEW
U of I CS 241 - Lecture notes

This preview shows page 1-2-3-24-25-26-27-48-49-50 out of 50 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 50 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C BasicsThe C Language SpiritCompilerProgramming in CSlide 5Slide 6VariablesSlide 8The “&” Operator: Reads “Address of”PointersThe “*” Operator Reads “Location pointed to by”What is the Output?Slide 13Slide 14Slide 15Slide 16Slide 17Cardinal Rule: Must Initialize Pointers before Using themSlide 19Slide 20How to Initialize PointersSlide 22Slide 23Slide 24Slide 25ArraysExampleArray Name as PointerSlide 29Example:Slide 31Question:Slide 33Strings (Null-terminated Arrays of char)String and char constantsString Operationsstrcpy, strlenSlide 38strncpySlide 40strcatstrcat ExamplestrcmpFormatted I/OMath: Increment and Decrement OperatorsSlide 46Math: Increment and Decrement Operators on PointersSlide 48Logic: Relational (Condition) OperatorsLogic ExampleCopyright ©: Nahrstedt, Angrave, Abdelzaher 1C BasicsTarek Abdelzaher and Vikram AdveCopyright ©: Nahrstedt, Angrave, Abdelzaher2The C Language SpiritMade by professional programmers for professional programmersVery flexible, very efficient, very liberalDoes not protect the programmers from themselves. Rationale: programmers know what they are doing even if looks bad enough to deserve a “Darwin award” (see http://www.darwinawards.com/)UNIX and most “serious” system software (servers, compilers, etc) are written in C.Can do everything Java and C++ can. It’ll just look uglier in CCopyright ©: Nahrstedt, Angrave, Abdelzaher3CompilergccSee manual “man” for options man gcc PreprocessorCompilerLinkerC89 versus C99C99: inline functions, variable length arrays, …make – a compilation utilityGoogle for make files (or GNU Make)Copyright ©: Nahrstedt, Angrave, Abdelzaher4Programming in CC = Variables + InstructionsCopyright ©: Nahrstedt, Angrave, Abdelzaher5Programming in CC = Variables + Instructionsintcharfloat…pointerarraystruct { …}Copyright ©: Nahrstedt, Angrave, Abdelzaher6Programming in CC = Variables + Instructionsintcharfloatstruct { …}…pointerarrayfunction callassignmentifswitch…forwhileCopyright ©: Nahrstedt, Angrave, Abdelzaher710,00010,00410,01210,01610,020…VariablesValue1Value2Value3Value4Value5xyzpdName (in program text)ValueMemoryint x;double y;float z;double* p;int d;Type of each variable(also determines size)Copyright ©: Nahrstedt, Angrave, Abdelzaher810,00010,00410,01210,01610,020…VariablesValue1Value2Value3Value4Value5xyzpdMemoryAddress(“name”at run-time)Name (in program text)ValueMemoryint x;double y;float z;double* p;int d;Type of each variable(also determines size)Copyright ©: Nahrstedt, Angrave, Abdelzaher910,00010,00410,01210,01610,020…The “&” Operator:Reads “Address of”Value1Value2Value3Value4Value5xyzpdNameValue&y MemoryCopyright ©: Nahrstedt, Angrave, Abdelzaher1010,00010,00410,01210,01610,020…PointersValue1Value2Value310,004Value5xyzpdNameValueA pointer is a variable whose value is a memory address:p = &y;MemoryCopyright ©: Nahrstedt, Angrave, Abdelzaher1110,00010,00410,01210,01610,020…The “*” OperatorReads “Location pointed to by”Value1Value2Value310,004Value5xyzpdNameValueA pointer is a variable whose value is the address of another:p = &y;*p MemoryCopyright ©: Nahrstedt, Angrave, Abdelzaher12What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);}Copyright ©: Nahrstedt, Angrave, Abdelzaher13What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);}#@*%!#@%$!@*%^pqxCopyright ©: Nahrstedt, Angrave, Abdelzaher14What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);}#@*%!#@%$!10pqxCopyright ©: Nahrstedt, Angrave, Abdelzaher15What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);}#@%$!10pqxCopyright ©: Nahrstedt, Angrave, Abdelzaher16What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);}#@%$!11pqxCopyright ©: Nahrstedt, Angrave, Abdelzaher17What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“q = %d\n“, q);}1111pqxCopyright ©: Nahrstedt, Angrave, Abdelzaher18Cardinal Rule: Must Initialize Pointers before Using themint *p;*p = 10;BADCopyright ©: Nahrstedt, Angrave, Abdelzaher19Cardinal Rule: Must Initialize Pointers before Using themint *p;*p = 10;#@*%!p??Pointing somewhererandomCopyright ©: Nahrstedt, Angrave, Abdelzaher20Cardinal Rule: Must Initialize Pointers before Using themint *p;*p = 10;#@*%!p#@*%!10Copyright ©: Nahrstedt, Angrave, Abdelzaher21How to Initialize PointersCopyright ©: Nahrstedt, Angrave, Abdelzaher22How to Initialize PointersSet pointer equal to address of known variableint *p;int x;…p=&x;Copyright ©: Nahrstedt, Angrave, Abdelzaher23How to Initialize PointersUse malloc()int *p;…p=(int*) malloc (sizeof (int)); malloc() allocates memory dynamically: “heap”sizeof(T) gives the size of a program type T.Copyright ©: Nahrstedt, Angrave, Abdelzaher24How to Initialize PointersCopy another initialized pointer valueint *p, *q;…q = (int*) malloc(sizeof(int));p = q;Copyright ©: Nahrstedt, Angrave, Abdelzaher25How to Initialize PointersCreate an Arrayint p[10];Same as:int *p;p=(int*) malloc (10*sizeof (int));Copyright ©: Nahrstedt, Angrave, Abdelzaher26Arraysint p[5]; p[0]p[1]p[2]p[3]p[4]Name of array (is a pointer)pShorthand:*(p+1) is called p[1]*(p+2) is called p[2]etc..Copyright ©: Nahrstedt, Angrave, Abdelzaher27Exampleint y[4];y[1]=6;y[2]=2; 62y[0]y[1]y[2]y[3]yCopyright ©: Nahrstedt, Angrave, Abdelzaher28Array Name as PointerWhat’s the difference between the examples below:Example 1:int z[8];int *q;q=z;Example 2:int z[8];int *q;q=&z[0];Copyright ©: Nahrstedt, Angrave, Abdelzaher29Array Name as PointerWhat’s the difference between the examples below:Example 1:int z[8];int *q;q=z;Example 2:int z[8];int *q;q=&z[0];NOTHING!!z (the array name) is a pointer to the beginning of the array, which is &z[0]Copyright ©: Nahrstedt, Angrave, Abdelzaher30Example:How much is y at the end:int y, x, *p;x = 20;*p = 10; y = x + *p;Copyright ©: Nahrstedt, Angrave, Abdelzaher31Example:How much is y at the end:int y, x, *p;x = 20;*p = 10; y = x + *p;BAD!! Dereferencing an unitialized pointerwill likely segfault or overwrite something!Segfault = unauthorized memory accessCopyright ©: Nahrstedt, Angrave, Abdelzaher32Question:What’s the difference betweenint* q;int q[5];What’s wrong with:int ptr[2];ptr[1] = 1;ptr[2] = 2;Copyright ©: Nahrstedt,


View Full Document

U of I CS 241 - Lecture notes

Documents in this Course
Process

Process

28 pages

Files

Files

37 pages

File I/O

File I/O

52 pages

C Basics

C Basics

69 pages

Memory

Memory

23 pages

Threads

Threads

14 pages

Lecture

Lecture

55 pages

C Basics

C Basics

24 pages

Signals

Signals

27 pages

Memory

Memory

45 pages

Threads

Threads

47 pages

Threads

Threads

28 pages

LECTURE

LECTURE

45 pages

Threads

Threads

30 pages

Threads

Threads

55 pages

Files

Files

37 pages

SIGNALS

SIGNALS

22 pages

Files

Files

37 pages

Threads

Threads

14 pages

Threads

Threads

13 pages

Load more
Download Lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?