I do a lot of work in various languages, most of which are scripting languages such as JavaScript, Shell Scripting, PHP and so on. But I do also work a lot with Java, which is closer to a more "real" language.
C/C++ has been an interest always, but have not had much use for it. I did have a few small things in some Java projects that needed to be delegated to a faster solution, mostly when dealing with a lot of file operations. For this I used C++ because it has some abstractions like string and such that let me avoid most of C's pointer styles "*".
But I did not really get all into C++, just read what I needed to make it work. And doing something without knowing fully what you are doing, can cause problems. For example, it's easy to create memory leaks without getting any problems with the compiler. Just because something compiles and runs, does not mean that it does not have problems.
So now I have decided to spend some time with this properly. And not C++, because you are still engaging C a lot of the time, so better to understand that one first, before starting with the other.
For the most part C is easy enough. What I find problematic is the pointers. I know what pointers is. But in most languages you work with references and not with pointers directly, so it is giving me a few headaches figuring out exactly what I am dealing with some times.
Example
char *cvar; cvar = ""; *cvar = "";
In the above example, I believe the first one is correct? The second one tries to change the pointer and not the content?
char *cvar = "";
But if that's the case, why is this then correct? Does the literals return values or pointers? Cause it seams as if it varies or I am mistaken about when I work with values or pointers.
function name(char *arg) { } name(&cvar);
C passes by value, so what do I get in the function? Copied pointer that points to the same place/value?
But if that's the case, why is this then correct?
-- Becausechar *cvar = "";
is both a declaration and an assignment, not just an assignment. You would never refer to an already-declared variable using "pointer to" (*) notation.so what do I get in the function?
-- You get a character identified byarg
, stored in a memory location scoped outside of the function.