General C programming questions

Posted by GregS on Tue 23 Aug 2005 08:52 PM — 4 posts, 19,767 views.

#0
I've done some java programming for school but, I'm new to the C programming language. They are similiar, but there are some major differences and I have a few questions:

1) What is the point of pointers? Do they just "point" to a memory location rather than manipulating the data itself? Is it more efficient or something? I'm confused as to exactly how these work.

2) "typedef struct affect_data AFFECT_DATA;"
What exactly does this do? Isn't it just renaming the struct affect_data to AFFECT_DATA? Or does this just allow you to use "AFFECT_DATA" instead of typing "struct affect_data"?

3) "#define MAX_KILLTRACK 25 /* track mob vnums killed */"
How does the program know whether MAX_KILLTRACK is a string, character, double, etc? Is it defined earlier or something?

More questions to follow...

Thanks
Greg


USA #1
Welcome to the C programming language... :-)


1) Pointers
Well, in Java, everything except for primitives (int, double, etc.) is actually a pointer. If you understand the notion of many pointers pointing to the same object, then you should be ok with C pointers.

C pointers have some interesting properties however. The most important of which is that you can change where a pointer points. An array - pointer to a block of memory - can be traversed by incrementing the pointer by one at a time until you reach the end of the array.

To 'manipulate the data itself' (funny statement, since sometimes the pointer is the 'data itself') you need to dereference the pointer. Basically, if you have a pointer to a class, you use p->method() instead of Java's p.method(). These are however doing similar things. It's also worth noting that C has no methods, but C++ does. In C all you have are 'instance variables' (structure members).

It's sort of hard to give a general, one-fell-swoop kind of answer to this question. Googling for pointers will give you an immense amount of information.

2) Typedef
When you write typedef x y, it means that y is an equivalent name for type x. (This syntax holds in all but function pointer typedefs, which are a little odd.) So, it is not renaming the struct per se, it's just saying that AFFECT_DATA is equivalent to type 'struct affect_data'.

3) #define
MAX_KILLTRACK isn't "anything" to the compiler. The reason is that it is preprocessed; before the compiler ever sees the source code, the preprocessor replaces all instances of MAX_KILLTRACK to 25. In some debug compilation modes, the macros - the preprocessor defines - are left intact, because it's useful to the programmer.

In any case, when you #define MAX_KILLTRACK 25, you are telling the preprocessor that whenever it sees MAX_KILLTRACK, replace it with 25. It is the definition. And yes, this is probably somewhat odd from a Java perspective.
Amended on Tue 23 Aug 2005 09:31 PM by David Haley
#2
Thanks for the help. This explanation was extremely helpful: "p->method() instead of Java's p.method()"

~Greg
USA #3
Pointers do just point to addresses. They are useful for dynamic data, data you need to initialize at run time, or for passing large constructs as arguments (via pointers).
Pointers are only (depending on the system and stuff) a byte or two, as opposed to passing an entire string, or array, or whatnot.
And they are also more efficient when iterating over arrays and things.
There are tons of other reasons to use pointers.

2, yeah. It allows you to use AFFECT_DATA as a struct (data type, basically).

3, definitions are basically text substitutions, done before compiling. So, technically 25 is just a two character, followed by a five character, and is used in whatever context it is.