C and C++ programming
Some facts regarding C/C++:
Actually both C and C++ were created to make the work of a programmer easier, not just to satisfy a programming language designer's goal. Prior to the existence of C, if you wanted to allocate memory in Assembler, it'd be a real pain. C allows that you leave this task to the compiler. Within embedded systems you need to inform the compiler your memory profile but within Windows or Unix this is not required, but your job remains significantly easier.
Before starting, if you could afford a C book I'd recommend that you acquired "The C Programming Language", by Kernighan and Ritchie. Although, you will find some outdated content ( the last edition of the book came out in 89 ) as the ANSI C standard was last updated in 2011. However, the book is fine. I don't have it though, but if you can afford and plan to learn C, buy it.
There is an excellent book that recommend, which I take with me anywhere I go is "C The Reference Manual 5th Edition" written by Samuel Harbison, which is careful enough to point the differences between all C versions, but I am not sure if it was updated to C11. ( Note I haven't used this book since 2006 ).
A pointer is nothing less than a variable that holds an address. You could be wondering why? If you're passing a whole struct to a function and will pass it by value ( i.e. not using pointers ) the compiler will have to copy it all. For single parameters this is not a trouble, but for huge arrays or huge structs this might be a trouble. With a few experience you start thinking about pointers as addresses and it is less scary with the years.
In fact, the pointer points by copying the address of the variable. If you increment a pointer, you just tell the compiler to jump to the next memory value. If your pointer points to int or float does not matter as it knows where to go. By the way, do never try a "&yourPointer++", because it will cause you major troubles. Either use:
Remember that & is the address and * is the content of the pointer. Or & gets the address and * the value we want.
So, go fast to the Ted Jensen's tutorial if you are troubled with pointers. As a matter of fact, he teaches a lot of tips, pratical ones, especially if you are dealing with char types ( which is what you usually do when dealing with tcp-ip, serial frames ).
Function pointers are also real hard to get IMHO, and it is generally avoided. However, I had to use it. In C++, if you want to have a generic function, you just use templates. In C, you cannot use it, so you'll have to use function pointers. Do I need to repeat that you should read the Ted Jensen's tutorial?
Say, you have such a function:
But you want to pass it a function. How to do it?
See that both functions have return types and parameters that are compatible to our function pointer. It is then natural to do:
The statement below is very complicated. As a matter of fact, I have always a book at my side when using a function pointer:
Ovo is a function that returns a int. Its parameters are an int value and a function pointer. Or, it gets a function that returns an int and has an int parameter ( check function1 and function2 ). Please note that we use the * operator in the protoype/definition of the function and not when we actually use the function. It's not required. Anyway, for further and better explanations please check Lars Haendel's tutorial on function pointers.
Another complex subject is dynamic allocation. Not because it's hard to understand, I don't think so, but if do not deallocate memory, perhaps you'll be overloading your memory capacities. C has many functions to allocate memory, and the mostused if malloc. It allocates N bytes from the heap, but you'll have to cast it to your desired type as it returns a pointer to char. If malloc returns NULL, you didn't get your goal.
The transition from C to C++ is not obvious, honestly, the only method I know to go through this process easily is to write code, laboriously and patiently. A good C programmer will learn C++ easily, because, a good C programmer will enjoy the improvements C++ has compared to C, and will feel motivated to go on.
C++ supports ( among others ) a paradigm named "Object Oriented Programming", which will make use of classes, encapsulation, polymorphism, etc. Again, be sure to use a book or tutorial that's ANSI C++ compliant otherwise you will find some troubles.
Bjarne Stroustrup has a great tip to detect if a tutorial or book is compliant: if the author's main function return void, discard it.
I highly recommend http://www.cplusplus.com - which has been also updated to C++11/C++14.
One of the most powerful features in C++ are templates and STL. There are several data containers that can be used. Programmers need not to worry to implement linked lists or queues. Programmers already have at hand several algorithms for searching, sorting etc. The limits of STL these day lie where on how much you know it. Believe me, STL at this point, is faster than any code one can write.
How do I learn C++?
First of all, be sure of knowing all the procedural programming, e.g, divide your programs in modules ( make use of prototypes ), make them easy to read. Eat pointers at breakfast. Next, take a deep breath. Start by trying to find real world troubles and making them C++ classes. Implement them, even if they're wrong. Submit them to friends that know C++ already. Eat class design at lunch. Next go for heritage and polymorphism. Heritage is the natural way of reusing a object or class. Polymorphism allows you to write less code when dealing with class hierarchies and also to plan your classes at design. Dive down on the standard template library, this actually very important.
To me the hardest part of C++ is operator overloading and at the same time this is my favorite feature of C++ and probably its most fascinating characteristic.
In your programs now, you must make interactions between objects, rather than passing parameters through functions. In another words, you must model every class to mimic a real world object and create a model that can be instantiated and that might have the role you need. One of the best features of the C++ language are its libraries and Standard Template Libraries. If you need a reference to ( not to learn it ) check this C++ reference and https://isocpp.org/ .
It is a good thing to have a reference to these libraries. They're handy enough to avoid you to write code that might've written, and for sure, better than you could write.
In C you can pass a variable by reference by using pointers. In C++, you have a different operator named & or reference operator.
The & operator simply creates an alias. Another new usage in C++ is how you allocate memory from the heap. In C, you should use malloc, calloc,etc, and later you would have to free the memory with free. C++ allows you to allocate memory from the heap using the "new operator". Say you have a class named Aclass.
By doing it, you're allocating memory from the stack. If you would like to allocate from the heap, use the new operator:
Ever heard of objects talking to each other by sending messages? The very functioning of good OOP means that you deal with objects that interact. Or, class A sends a message to class B through a method. When you're designing a class, have in mind that's necessary to deallocate every object you put in the heap, so use the destructor.
One of my favorite topics in OOP/C++ is heritage and polymorphism. Perhaphs, because I took some time to digest the C++ way of doing it. We will start with a class hierarchy as simple as possible:
After having, a derivation set, we need to implement it. Next, you need a class that deals with these classes. This class must use polymorphism to access a class. In terms of OOP, we might say that this class will receive a message from another class.
The STL strings, usually found under the header <strings>, and usually noted std::strings are very useful. They will save a lot of time and they are faster enough to make you to forget ANSI C NULL terminated strings.
The C++11 and C++14 new features not just make it C++ more appealing but faster and safer. Smart pointers, auto types, lambda functions, move constructors, etc.