(2) ArkM (IS/IT--Management) |
7 Feb 12 5:24 |
Some additions: 1. There is a wonderful operator in C. It's defined as CODEa[expr] <=> *(a+(expr)) Now you may write much more clear codes with CODEp[i] /*instead of correct but cumbersome *(p+i) */ 2. String literal "..." is a special kind of array of chars in C. The content of "abc" is exactly the same as CODE{ 'a', 'b', 'c', '\0' } /* constant array of four chars */ By historical reasons it's possible to get non-constant pointer to this array in C but any correct program can't assign values via such a pointer. Now let's remember that an array is implicitly converted to the pointer to the 1st element of this array (everywhere except sizeof and address of (unary &) operators argument). That's why you can write this declaration: CODEchar* q = "MANICHANDRA"; /* Better write more correctly as below: */ const char* q = "MANICHANDRA"; Now q is a pointer to the 1st character of (constant!) array of chars (to 'M' letter in this case). Compare with the different construct: This is a special kind of an array declaration with initialization. After that s is a name of array of 4 characters. It's the same as CODEchar s[4] = { 'M', 'A', 'N', '\0' }; /* see zero byte terminator */ Feel free to change all four elements of s: CODEs[0], s[1], s[2], s[3] 3. Function arguments are passed "by value" in C. In other words, a function receives copies of argument values. Let's consider CODEvoid f(char* p) { char s[] = "MAN"; p = s; } ... char ss[] = "WOMAN"; f(ss); printf("%s\n",ss); /* prints WOMAN */ We have passed a copy of a pointer to ss. Now (in the function body) p is a name of this copy - local pointer variable. After that the function assigns another value (a pointer to the local array s) to this copy (parameter) variable. It has no relation to ss argument. The function f does nothing visible outside its scope! May be it helps you to understand your snippets. The last advice: try to get the best book about C - The C Programming Language by K&R. Good luck! |
|