Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the important notions in C that you did not learn from your teachers? [closed]

Tags:

c

Use of const keyword in pointers context:

The difference between following declarations:

 A)   const char* pChar  // pointer to a CONSTANT char
 B)   char* const pChar  // CONSTANT pointer to a char
 C)   const char* const pChar  // Both

So with A:

const char* pChar = 'M';
*pChar = 'S'; // error: you can't modify value pointed by pChar

And with B:

char OneChar = 'M';
char AnotherChar = 'S';
char* const pChar = &OneChar;
pChar = &AnotherChar; // error: you can't modify address of pChar

My teachers spent so much time teaching us that pointers are scary little goobers that can cause lots of problems if not used correctly, that they never bothered to show us how powerful they can really be.

For example, the concept of pointer arithmetic was foreign to me until I had already been using C++ for several years:

Examples:

  • c[0] is equivalent to *c
  • c[1] is equivalent to *(c + 1)
  • Loop iteration: for(char* c = str; *c != '\0'; c++)
  • and so on...

Rather than making students afraid to use pointers, teach them how to use them appropriately.

EDIT: As brought to my attention by a comment I just read on a different answer, I think there is also some value in discussing the subtle differences between pointers and arrays (and how to put the two together to facilitate some pretty complex structures), as well as how to properly use the const keyword with respect to pointer declarations.


They really should learn to use helper tools (i.e. anything other than the compiler).

1) Valgrind is an excellent tool. It's phenomenally easy to use and it tracks down memory leaks and memory corruption perfectly.

It'll help them understand C's memory model: what it is, what you can do, and what you shouldn't do.

2) GDB + Emacs with gdb-many-windows. Or any other integrated debugger, really.

It'll help those that are to lazy to step through the code with pencil and paper.


Not really restricted to C; here's what I think they should learn:

1) How to properly write code: How to write unmaintainable code. Reading that, I found at least three crimes I was guilty of.

Seriously, we write code for other programmers. Thus, it's more important for us to write clearly than it is to write smartly.

You say your students aren't actually programmers (they're engineers). So, they shouldn't be doing tricky things, they should focus on clear coding.

2) STFW. When I started programming (I started in Pascal, than moved to C), I did it by reading books. I spent countless hours trying to figure out how to do stuff.

Later on, I found that everything I had had to figure out had already been done by many others, and at least one of them had posted it online.

Your students are engineers; they don't have as much time to devote to programming. So, the little time they have, they should spend reading other people's code and, maybe, brushing up on idioms.


All in all, C's a pretty easy language to learn. They'll have a lot more trouble writing anything longer than a few lines than they'll have learning independent notions.


When I had to use C as part of a larger project in school it was the ability to use gdb properly (i.e. at all) that ended up predicting who would finish their project and who would not. Yeah if things get crazy and you have tons of pointer and memory related bugs gdb will show weird information but even knowing that can point people in the right direction.

Also reminding them that C isn't C++, Java, C#, etc. is a good idea. This comes up most frequently when you see someone treating a char* like a string in C++.