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 SpiritMade by professional programmers for professional programmersVery flexible, very efficient, very liberalDoes 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, Abdelzaher3CompilergccSee manual “man” for options man gcc PreprocessorCompilerLinkerC89 versus C99C99: inline functions, variable length arrays, …make – a compilation utilityGoogle 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 PointersSet pointer equal to address of known variableint *p;int x;…p=&x;Copyright ©: Nahrstedt, Angrave, Abdelzaher23How to Initialize PointersUse 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 PointersCopy another initialized pointer valueint *p, *q;…q = (int*) malloc(sizeof(int));p = q;Copyright ©: Nahrstedt, Angrave, Abdelzaher25How to Initialize PointersCreate 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 PointerWhat’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 PointerWhat’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