Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ General C programming questions

General C programming questions

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by GregS   (15 posts)  Bio
Date Tue 23 Aug 2005 08:52 PM (UTC)
Message
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


Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 23 Aug 2005 09:24 PM (UTC)

Amended on Tue 23 Aug 2005 09:31 PM (UTC) by David Haley

Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by GregS   (15 posts)  Bio
Date Reply #2 on Tue 23 Aug 2005 09:27 PM (UTC)
Message
Thanks for the help. This explanation was extremely helpful: "p->method() instead of Java's p.method()"

~Greg
Top

Posted by Flannel   USA  (1,230 posts)  Bio
Date Reply #3 on Tue 23 Aug 2005 09:29 PM (UTC)

Amended on Tue 23 Aug 2005 09:31 PM (UTC) by Flannel

Message
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.

~Flannel

Messiah of Rose
Eternity's Trials.

Clones are people two.
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


15,852 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.