Do people really think C is some mysterious, inscrutable language? "To many programmers, this makes C scary and evil." Is this actually true for people? I find C code generally very easy and straightforward to understand; there's not any magic behind the scenes, like there is in any language that's more "high level" than C.
Many of the friends I made in my CS classes were terrible with pointers. I never really understood why they didn't grasp pointers, but it was a major stumbling block for them in C/C++
I never really understood why they didn't grasp pointers
The root of the problem is the language designers' loose use of star. "star something" is contained in a phrase that means one thing at declaration, "star something" has a different meaning the rest of the time. #include
void eg(int i) {
int *j = &i; // "huh? Put the address of i into *j?"
*j = *j + 1;
printf("%d\n", *j);
}
int main() {
eg(4);
}
With more detail. In the line.. int *var = something;
.. the system assigns to the pointer. Yet in.. *var = 6;
.. it assigns to the contents of the pointer.Common usage creates further room for confusion:
int *var; //
If they'd made the syntax ".int var", and then used * solely for dereferencing, people wouldn't have these problems learning pointers. Consider #include
void eg(int i) {
.int j = &i;
*j = *j + 1;
printf("%d\n", *j);
}
int main() {
eg(4);
}
Further confusion comes from (1) special arrangements around string declaration and (2) printf use of %s to expect a string pointer when %d and %f expects (non-pointer) simple int and simple float. char* something = "huh? so now this does goe into *something?";