.
carbonize.co.ukcarbonize.co.uk

Y!Tunnel
Trillian
YahELite
Opera
Ad Aware
Ultraedit
YahSEek

Advertisements:


Send honey to your loved ones

[Get Opera!]

Synfire's Guide to C Programming
10. struct

    Now this shit gets interesting. Remember the descussion about TYPES earlier in this tutorial? Well, what if the predeterminded types just aren't good enough for you? That's were struct comes in. Struct allows you to create your own type. Check out this example and be sure to read the comments closely.

struct.c
--------------------------------
#include <stdio.h>
#define MAX 20

struct New /* creates a new type called 'New'
{
	char *Handle[MAX]; /* creates subtypes of the new struct */
	char *Name[MAX];
	unsigned int Age;
};

int main()
{
	New Hacker; /* creates a variable 'Hacker' of type 'New'
		       now 'Hacker' contains all the parts in
		       'New'. */
	printf("Enter Your Name: ");
	scanf("%s", Hacker.Name); /* assigns user input to the 'Hacker' 'Name'
				     subtype. Each subtype can be accessed
				     just like another variable. */
	printf("Enter Your Handle: ");
	scanf("%s", Hacker.Handle);
	printf("Enter Your Age: ");
	scanf("%d", Hacker.Age);

	printf("Welcome %s AKA %s.\n", Hacker.Name, Hacker.Handle);
	if(Hacker.Age <= 20)
		printf("Your too young to be around this shit kid!\n")
	if(Hacker.Age >= 22)
	{
		printf("Do you remember when ARPA-Net was trashed by ");
		printf("the government?\n");
	}
	if(Hacker.Age == 21)
		printf("All drinks on %s tonight!\n", Hacker.Handle);
	return(0);
}
---------------------------------
        So you might say that's kewl, but why not just define them seperately? Well, the answer is, that in struct you don't only define variables and arrays, you can also define functions. And with seperate declaration you can only use it once, but with a new type you can create an array of that type so that you can assign the same variables different values as many times as you want without corrupting the original value. I have found this very usefull in creating Database programs and Security programs in which many people might login at once. Check this out...
New Hackers[80];
    If I would have used that instead of just the one then I would have 80 seperate Hackers that I could manipulate individually instead of just writing...
New Hacker1;
New Hacker2;
New Hacker3;
...
New Hacker80;
    That would be really cumbersome and would really lag your program.
So if you need more than one I suggest making an array of them, or if wanna get real AWOL with them try to create an army of Hackers...
New Hacker[10][10][10][10];
    That would be kewl!~ ;)


Back to top | Contact me