Presentation is loading. Please wait.

Presentation is loading. Please wait.

. Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1.

Similar presentations


Presentation on theme: ". Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1."— Presentation transcript:

1 . Plab – Tirgul 2 Const, C Strings

2 Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1

3 Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1 0x0100

4 Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1 0x0100 1

5 u C’s “const” is a qualifier that can be applied to the declaration of any variable to specify its value will not be changed. const double e = 2.71828; const char msg[] = “Warning: ”; msg[1] = ‘w’; // illegal ! C ’s “const”

6 u Do not confuse what the “const” declaration “protects” !  A pointer to a const variable: int const * p = {1,2,3}; p[1] = 1; // illegal! *(p+1) = 1; // illegal! p = NULL; //legal  A const pointer to a variable: int* const const_p = {1,2,3}; const_p[1] = 0; // legal ! const_p = NULL; // illegal! C ’s “const” 321 321

7 Pointer’s Syntax  Compare: (1)int * const p = {1,2,3};//ok (2)const int * p = {1,2,3};//bad style! (3)int const * p = {1,2,3};//ok u (2) and (3) are synonyms in C to a pointer to a const array. We encourage right to left reading of declarations, to achieve better readability and avoid errors.

8 Pointers and User Defined Types struct Complex { int img; int real; }; Complex const comp1 = comp2; //ok,initialize using comp2 comp1.img = 3; // illegal ! comp1 value is constant  All the members of a const variable are immutable !

9 Compare to Java’s “final” u All (methods as well as data ) are Class members. u “final” makes primitive types constants and references to objects constant. u The values inside the referred objects are not constant ! final int LIMIT = 10; int LIMIT = 11;// illegal ! final MyObject obj1 = MyObject(); MyObject obj2 = NULL; MyObject obj3 = MyObject(); obj2 = obj1;//fine, both point now to the same object obj1 = obj3; // illegal ! obj1.setSomeValue(5); // legal ! * Because All are class members you would normally use them as class constants and declare them as “static final”

10 “Const” Usage u The const declaration can (and should !) be used in the definition of a function’s arguments, to indicate it would not change them: int strlen(const char []); u Why use ? (This is not a recommendation but a must)  Clearer code  Avoids errors  Part of the interfaces you define!  We will see more of “const” meaning and usage when we get to C++

11 Strings in C

12 String & Chars: C vs. Java u Java: Char: is 2 bytes (Unicode). String: an object which behaves as a primitive type (an immutable object, passed by value)  C: Char: usually 1 byte. An Integer. String: An array of characters. char* txt1 = “text”; char txt2[] = “text”; char txt3[] = {‘t’,’e’,’x’,’t’,’\0’}; xett\0xett

13 C Strings u Strings are always terminated by a null character, (a character with integer value 0). u There is no way to enforce it when you do your own strings →:  Remember it’s there  Allocate memory for it char* text = “string”; // means text[5] == ‘g’ and text[6] == ‘\0’ // 7 chars are allocated!

14 C Strings Examples u Recall arrays (as learned in Class2) are essentially constant pointers. char txt1[] = “text”; char* txt2 = “text”; int i = strlen(txt1); // i = 4, same for strlen(txt2); txt1[0] = ‘n’; //”next”; txt1 = txt2; // illegal ! txt2 = txt1; //legal. now txt2 points to the same string. if ( ! (strcmp(txt2,”next”)) //This condition is now true

15 C Strings Manipulation u To manipulate a single character use the functions defined in ctype.h #include char c = ‘A’; isalpha(c); isupper(c); islower(c); …. u Manipulation of Strings is done by including the string.h header file // copy a string char* strcpy(char * dest,const char* src); // append a string char* strcat(char * dest,const char* src);

16 C Strings Manipulation (2) // compare two strings. //when str1 < str2 lexicographically return < 0 //when str1 0 //when identical return 0 int strcmp(const char * str1, const char* str2); // return string’s length, not including the ‘\0’ !! size_t strlen(const char * str); Other functions: strncpy(),strncat(),strcmp() ……… NOTE : All C library functions assumes the usages of ‘\0’ and enough storage space. No boundary checks ! You are responsible.

17 C Strings Functions u An “array” version of strcpy(): void strcpy(char * dest,const char* src){ int i =0; while ((dest[i] = src[i]) != ‘\0’)) i++; } u A “pointers” version of strcpy(): void strcpy(char * dest,const char* src){ while ((*dest = *src) != ‘\0’)) { dest++; src++; }

18 C Strings Functions (2) u An experienced C programmer would write: void strcpy(char * dest,const char* src){ while ((*dest++ = *src++) != ‘\0’)) ; }  Actually the comparison against ‘\0’ is redundant: void strcpy(char * dest,const char* src){ while (*dest++ = *src++) ; }  Style note: Unlike K&R book, we do NOT encourage you to write such code. However, you should be able to read and understand it. The language features are used in any case.

19 C Strings Functions u What does this do? What are the assumptions ? int f(const char * p,const char * q) { for(;*p && *p ==*q; p++,q++) ; return *p-*q; }


Download ppt ". Plab – Tirgul 2 Const, C Strings. Pointers int main() { int i,j; int *x; // x points to an integer i = 1; x = &i; j = *x; ijx 1."

Similar presentations


Ads by Google