1Copyright ©: Nahrstedt, Angrave, Abdelzaher 1C BasicsTarek AbdelzaherCopyright ©: 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 C2Copyright ©: Nahrstedt, Angrave, Abdelzaher3Compiler gcc See manual “man” for options man gcc Preprocessor Compiler Linker C89 versus C99 C99: Mix variable declarations and code make – a compilation utility Google for make files (or GNU Make)Copyright ©: Nahrstedt, Angrave, Abdelzaher4Programming in CC = Variables + Instructions3Copyright ©: Nahrstedt, Angrave, Abdelzaher5Programming in CC = Variables + Instructionsintcharfloatstring…pointerarrayCopyright ©: Nahrstedt, Angrave, Abdelzaher6Programming in CC = Variables + Instructionsintcharfloatstring…pointerarrayprintf/scanfassignmentifswitch…forwhile4Copyright ©: Nahrstedt, Angrave, Abdelzaher710,00010,00210,00810,01010,012…VariablesValue1Value2Value3Value4Value5xyzpdMemoryAddressNameValueCopyright ©: Nahrstedt, Angrave, Abdelzaher810,00010,00210,00810,01010,012…The “&” Operator:Reads “Address of”Value1Value2Value3Value4Value5xyzpdNameValue&y5Copyright ©: Nahrstedt, Angrave, Abdelzaher910,00010,00210,00810,01010,012…PointersValue1Value2Value310,002Value5xyzpdNameValueA pointer is a variable whose value is the address of anotherCopyright ©: Nahrstedt, Angrave, Abdelzaher1010,00010,00210,00810,01010,012…The “*” OperatorReads “Variable pointed to by”Value1Value2Value310,002Value5xyzpdNameValueA pointer is a variable whose value is the address of another*P6Copyright ©: Nahrstedt, Angrave, Abdelzaher11What 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, Abdelzaher12What is the Output?main() {int *p, q, x;x=10;p=&x;*p=x+1;q=x;printf (“Q = %d\n“, q);}#@*%!#@%$!@*%^pqx7Copyright ©: 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);}#@*%!#@%$!10pqxCopyright ©: 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);}#@%$!10pqx8Copyright ©: 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);}#@%$!11pqxCopyright ©: 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);}1111pqx9Copyright ©: Nahrstedt, Angrave, Abdelzaher17Cardinal Rule: Must Initialize Pointers before Using themint *p;*p = 10;BADCopyright ©: Nahrstedt, Angrave, Abdelzaher18Cardinal Rule: Must Initialize Pointers before Using themint *p;*p = 10;#@*%!p??Pointing somewhererandom10Copyright ©: Nahrstedt, Angrave, Abdelzaher19Cardinal Rule: Must Initialize Pointers before Using themint *p;*p = 10;#@*%!p#@*%!10Copyright ©: Nahrstedt, Angrave, Abdelzaher20How to Initialize Pointers11Copyright ©: Nahrstedt, Angrave, Abdelzaher21How to Initialize Pointers Set pointer equal to location of known variableint *p;int x;…p=&x; Copyright ©: Nahrstedt, Angrave, Abdelzaher22How to Initialize Pointers Use malloc()int *p;…p=(int*) malloc (sizeof (int));12Copyright ©: Nahrstedt, Angrave, Abdelzaher23How to Initialize Pointers Create an Arrayint p[10];Same as:int *p;p=(int*) malloc (10*sizeof (int)); Copyright ©: Nahrstedt, Angrave, Abdelzaher24Arraysint 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..13Copyright ©: Nahrstedt, Angrave, Abdelzaher25Exampleint y[4];y[1]=6;y[2]=2;62y[0]y[1]y[2]y[3]yCopyright ©: Nahrstedt, Angrave, Abdelzaher26Array 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];14Copyright ©: Nahrstedt, Angrave, Abdelzaher27Array 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!!x (the array name) is a pointer to the beginning of the array, which is &x[0]Copyright ©: Nahrstedt, Angrave, Abdelzaher28Example:How much is y at the end:int y, x, *p;x = 20;*p = 10; y = x + *p;15Copyright ©: Nahrstedt, Angrave, Abdelzaher29Example: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, Abdelzaher30Question: What’s the difference betweenint* q;int q[5]; What’s wrong with:int ptr[2];ptr[1] = 1;ptr[2] = 2;16Copyright ©: Nahrstedt, Angrave, Abdelzaher31Question: What is the value of b[2] at the end?int b[3];int* q;b[0]=48; b[1]=113; b[2]=1;q=b;*(q+1)=2;b[2]=*bb[2]=b[2]+b[1];Copyright ©: Nahrstedt, Angrave, Abdelzaher32Strings (Null-terminated Arrays of Char) Examplechar s[5]; Strings are arrays that contain the string characters followed by a “Null” character to indicate end of string. Do not forget to leave room for the null characters[0]s[1]s[2]s[3]s[4]s17Copyright ©: Nahrstedt, Angrave, Abdelzaher33ConventionsStrings “string” “c”Character ‘c’Copyright ©: Nahrstedt, Angrave, Abdelzaher34String Operations strcpy strlen srtcat strcmp18Copyright ©: Nahrstedt, Angrave, Abdelzaher35strcpy, strlenSyntax: strcpy(ptr1, ptr2);where ptr1 and ptr2 are pointers to char value = strlen(ptr);where value is an integer andptr is a pointer to char Example:int len; char str[15]; strcpy (str, "Hello, world!"); len = strlen(str); Copyright ©: Nahrstedt, Angrave, Abdelzaher36strcpy, strlenWhat’s wrong with char str[5]; strcpy (str, "Hello");19Copyright ©: Nahrstedt, Angrave, Abdelzaher37strncpySyntax: strncpy(ptr1, ptr2, num);where ptr1 and ptr2 are pointers to charnum is the number of characters to be copiedExample:int len; char str1[15], str2[15]; strcpy (str1, "Hello, world!"); strncpy (str2, str1, 5); Copyright ©: Nahrstedt, Angrave, Abdelzaher38strncpySyntax: strncpy(ptr1, ptr2, num);where ptr1 and ptr2
View Full Document