LCTHW, opinions & a question!

Started by
3 comments, last by blewisjr 11 years, 4 months ago
Hello!

I introduced myself couple of days ago.

I've been recommended to use The C Programming Language book, but I plan to use it later on since I've already started with something else. That something else is the Zed Shaw "tutorial", Learn The Hard Way.

I don't know if you guys know it but I'd like to know what's your opinion about it.

I used it to learn Python too, alongside with Invent Your Own Games With Python tutorial, and it worked, but for C I'm finding it harder, I suppose it has to be with C harder nature!

Apart from that I have another question, but since it's language-related and not game-related I hope I'm not doing wrong.

What is the difference between a pointer and a variable? As far as I've seen the uses of pointers in the tutorial I told you, they seem to work the same way. In the tuto he mentions something about bigger amounts of info, maybe I should get to structs (i don't know yet what that is :D) to understand it better? Some light over the matter would be very much appreciated!

Thanks in advance and regards!

N.
Advertisement

What is the difference between a pointer and a variable?

A variable is basically a memory block or data object. You can read from it, and generally you can write to it. Variables can be of many different sizes and types.

A pointer is just one of those types of variables. It is used to point to another object.


Edit: Add an example:

char a; /* A variable that holds a character */
int b; /* A variable that holds an integer */
MyStruct c; /* A variable that holds a structure */

char * x; /* A variable that points to a character */
int * y; /* A variable that points to an integer */
MyStruct * z; /* A variable that points to a structure */


In all cases (because you specified this is the C language) all the variables are uninitialized. You will need to fill in valid values for a, b, and c. You will also need to point x, y, and z to valid objects.

a = 12; /* set a to a value, specifically a numeric constant */
x = &a; /* set x to a value, specifically the address of another variable */

char a; /* A variable that holds a character */
int b; /* A variable that holds an integer */
MyStruct c; /* A variable that holds a structure */

char * x; /* A variable that points to a character */
int * y; /* A variable that points to an integer */
MyStruct * z; /* A variable that points to a structure */


And just to expand a little on this explanation, a pointer is just a variable that holds a memory address. Example:

Given the following declaration, the variable 'a' is shorthand for a memory address that contains the value 5.

int a = 5;

During execution, the value 5 is read directly from the address to which 'a' refers. Like in this case.

int a1 = a;

Here, the value stored at the location represented by 'a' is copied into another address represented by 'a1'. Contrast that with the following pointer declaration, which is going to be interpreted by the compiler in a very different way.

int *b = &a;

'b' is also shorthand for a memory address, but the * tells the compiler to treat it as a pointer, which means that any values found at this address are themselves memory addresses. There are two ways to use a pointer. One is to read the value directly, i.e. the memory address it contains. For example:

int *c = b;

This is saying, take the address stored in b and copy it to c. Both b and c now point to the same thing, in this case 'a' since I assigned its address to b above. And the other is to read the value stored at the address it points to:

int d = *b;

Here, we are reading the address stored in b (the address of a) and then jumping to that address and reading the value there. So, given the assignments above, d will be set to 5.

So pointers are variables, just as non-pointers are. The only difference is that the values they represent are memory addresses rather than integers, characters, or other types.
Thank you! Much clearer now! :)))
Just hang in there C is actually a quite simple language once you get use to how pointers work. The main reason pointers exist in C is that there is no way to pass data by reference to a function otherwise. In C everything is passed by value (copied). By using Pointers it allows you to pass a memory address to the function so that you don't need to copy that data on being passed. One such example would be passing a integer to a function but you want that function to modify the integer you passed in. Why really depends but often it can be used to return multiple peices of data from a function.

The K&R book (The C Programming Language) Is the best book you can possibly have. I used this book to learn C. It was written by the creators of the language. I have no expierence with Learn C The Hard Way but another great book is C Primer by Prata (same author of the renowned C++ Primer Plus book) C Primer has tons of more pages compaired to the K&R book but it has it's reasons due to the way material is presented. Prata does a great job at going into detail without getting heavily technical which can make it a better book for somone who never programed before.

This topic is closed to new replies.

Advertisement