Okay... here's a lesson for today regarding C. Which is still up to now someone come to me and ask what the heck happen in my code. believe me whether you will find yourself working 5 to 10 years in C without taking a serious understanding on this will condemn you to repeat this mistakes...
1. What's wrong on the code below?
PHP Code:
int *pVar1;
pVar = 0
*pVar1 = 32676;
2. What about this one? still in pointers.
PHP Code:
*p = (char *) malloc(100);
*p = 'y';
3. They've been teaching us that C++ is superset of C...which I guess not TRUE!... like the code below,
PHP Code:
int *p = malloc(sizeof(int) * 1000);
it is valid in C then why isn't valid in C++? how would I write so it would be valid in both C and C++?
This is just one of the reason why the core dll's in microsoft still in C language...and there's no way they can make it in C++. Even in embedded devices... just think about "void *" Why,When,Where and How...
4.Are expressions of var_array and *var _array same in array of integers? *sigh*
5. lastly,
PHP Code:
int main() {
/*NOTE 1: We defined here local variables */
int var_int, var_array[2];
/* NOTE 2: defined our pointers the one will hold the int the other one holds the array*/
int *p1, *p2;
/* NOTE 3: Finally! p1 points to our variable Integer */
p1 = &var_int;
/* NOTE 4: Finally! p2 points to our variable Array at index 0 */
p2 = &var_array[0];
/* Now let's assign some values */
*p1 = 3; /* var_int now is equal to 3... please see NOTE: 3 */
*++(p2) = 1; /* Can you determine what happen now in our var_array???? where the int "1" being stored... See NOTE 4 */
return 0;
}
Having lack knowledge of "operator precedence rules" and "dereference operator" in C or C++ would let anyone confuse! This topic is usually in Chapter 1 in your book... which surely be forgetten when you reach CLASSES & OBJECTS...I was been asked way back in my interview where I was not allowed to run the code since it is clearly readable... I haven't answer... how lame I was.... I was thinking that they would ask me some few algorithms or solve some problem... I was confident enough since I know how to capture exceptions or hit F1 in any IDE's and scan proper arguments.... well, I Learn my lessons, which I guess worth to be shared.